首页 文章 精选 留言 我的

精选列表

搜索[双向转换],共10000篇文章
优秀的个人博客,低调大师

Android 常用的地球经纬度转换公里(km)计算工具类

地球赤道上环绕地球一周走一圈共40075.04公里,而@一圈分成360°,而每1°(度)有60,每一度一秒在赤道上的长度计算如下: 40075.04km/360°=111.31955km 111.31955km/60=1.8553258km=1855.3m 任意两点距离计算公式见代码: package org.wavefar.lib.utils; import android.content.Context; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException; import com.baidu.mapapi.navi.BaiduMapNavigation; import com.baidu.mapapi.navi.NaviParaOption; import com.baidu.mapapi.utils.poi.BaiduMapPoiSearch; import com.baidu.mapapi.utils.route.BaiduMapRoutePlan; import com.baidu.mapsdkplatform.comapi.location.CoordinateType; import org.wavefar.lib.utils.LogUtil; import org.wavefar.lib.utils.StringUtil; /** * 定位相关工具类 * @author summer */ public class LocationUtil { private LocationClient mLocationClient; private BDAbstractLocationListener mlistener; private Context context; // 地球半径 private final static double EARTH_RADIUS = 6378.137; public LocationUtil(Context context) { this.context = context; mLocationClient = new LocationClient(context.getApplicationContext()); LocationClientOption option = initLocationOption(); mLocationClient.setLocOption(option); LogUtil.d(LocationUtil.class.getSimpleName(), "当前定位库版本" + mLocationClient.getVersion()); } private LocationClientOption initLocationOption() { LocationClientOption option = new LocationClientOption(); // 设置定位模式 option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); // 返回的定位结果是百度经纬度,默认值gcj02 option.setCoorType(CoordinateType.BD09LL); // 设置发起定位请求的间隔时间为5000ms option.setScanSpan(5000); // 返回的定位结果包含地址信息 option.setIsNeedAddress(true); //可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIsNeedLocationPoiList(true); // 返回的定位结果包含手机机头的方向 option.setNeedDeviceDirect(true); //可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationDescribe(true); option.setOpenGps(true); return option; } /** * 注册定位回调 * @param listener */ public void registerLocationListener(BDAbstractLocationListener listener) { mlistener = listener; if (mlistener != null) { mLocationClient.registerLocationListener(mlistener); } } /** * 启动定位 */ public void start() { if (mLocationClient.isStarted()) { return; } mLocationClient.start(); } /** * 重新定位 */ public void reStart() { if (mLocationClient.isStarted()) { return; } mLocationClient.restart(); } /** * 停止定位 */ public void stop() { if (mLocationClient.isStarted()) { mLocationClient.stop(); } } /** * 销毁定位 */ public void destroy() { if (mLocationClient != null) { mLocationClient.unRegisterLocationListener(mlistener); mLocationClient.stop(); mLocationClient = null; } } /** * 启动百度地图导航(Native),如果本地没有安装百度地图,调用WEBAPP * @param pt1 起点 * @param pt2 终点 */ public void startNavi(LatLng pt1, LatLng pt2) { // 构建 导航参数 NaviParaOption para = new NaviParaOption() .startPoint(pt1).endPoint(pt2); try { BaiduMapNavigation.openBaiduMapNavi(para, context); } catch (BaiduMapAppNotSupportNaviException e) { BaiduMapNavigation.openWebBaiduMapNavi(para, context); e.printStackTrace(); } } /** * 启动百度地图导航(Web) */ public void startWebNavi(LatLng pt1,LatLng pt2) { NaviParaOption para = new NaviParaOption() .startPoint(pt1).endPoint(pt2); BaiduMapNavigation.openWebBaiduMapNavi(para, context); } /** * 用完导航后需要调用该函数销毁 */ public void stopNavi() { try { BaiduMapNavigation.finish(context); BaiduMapRoutePlan.finish(context); BaiduMapPoiSearch.finish(context); } catch (Exception e) { e.printStackTrace(); } } private static double rad(double d) { return d * Math.PI / 180.0; } /** * 计算距离 返回单位km * @param lat1 * @param lng1 * @param lat2 * @param lng2 * @return */ public static double getDistance(double lat1, double lng1, double lat2, double lng2) { LogUtil.d("LocationUtil", "lat1:" + lat1 + "lng1:" + lng1 + "lat2:" + lat2 + "lng2:" + lng2); double radLat1 = rad(lat1); double radLat2 = rad(lat2); // 如果有一方等于零,直接返回0 if (radLat1 == 0 || radLat2 == 0) { return 0; } double a = radLat1 - radLat2; double b = rad(lng1) - rad(lng2); double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); s = s * EARTH_RADIUS; s = Math.round(s * 10000) / 10000; return s; } /** * 距离格式化 * * @param distance 以千米为单位 * @return */ public static String distanceKMFormat(double distance) { return distance > 1 ? (distance + "KM") : (distance * 1000 + "M"); } /** * 距离只保留两位小数 * @param distance 以米为单位 * @return */ public static String distanceFormat(double distance) { String str; double value = distance; if (distance >= 1000) { value = value / 1000; str = "KM"; } else { str = "M"; } return String.format("%s%s",StringUtil.formatDecimal(value,null),str); } } 以上代码定位依赖于百度定位API,所以粘贴以上代码之前,需要引入百度定位API,引入百度定位API不在这里介绍,自行去百度定位官方查看;

优秀的个人博客,低调大师

巧用fastjson自定义序列化类实现字段的转换

项目中突然需要增加另一个字段的查找,而这个查找需要查另一张表的记录。 但现在产品很多地方都要增加该字段,如何最快的实现该功能呢。 办法如下: 通过fastjson序列化时,增加该字段的序列化类,该序列化类通过CODE查找名称,并序列化到前端。 VO的功能修改 @Data public class SysDictItemSelectVo implements Serializable{ private static final long serialVersionUID = 1L; @JSONField(serializeUsing=ToSystemNameSerializer.class) private String systemName; public String getSystemName(){ return this.getCode(); } } 序列化的类的属性 import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.ObjectSerializer; import com.alibaba.fastjson.serializer.SerializeWriter; import java.io.IOException; import java.lang.reflect.Type; public class ToSystemNameSerializer implements ObjectSerializer { public static final ToSystemNameSerializer instance = new ToSystemNameSerializer(); @Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.out; if (object == null) { out.writeNull(); return; } //原来的值code String strVal = object.toString(); System.out.println(strVal); //通过code查找相关名称,测试先写死值 String name="std"; out.writeString(name); } } 经测试,序列化正常,其他地方只需要在VO增加该注解即可.

优秀的个人博客,低调大师

使用Jackson来实现java对象和json格式的相互转换

在经过数据库持久化,网络编程,多线程了解之后, 可以进入一些开发技术了。 当然,数据结构,算法,开发模式,第三方库的了解也是必须的。 疯狂软件出的《Spring Mybatis企业应用实战》, 作为操作解说型的教程, 还是不错的! 以下几个文件,是为了怕自己删除了没地方找回来而存在的。 web.xml <?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> springmvc-config.xml <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="org.fkit.controller"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/content/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> Book.java package org.fkit.domain; import java.io.Serializable; public class Book implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer id; private String name; private String author; public Book() { super(); } public Book(Integer id, String name, String author) { super(); this.id = id; this.name = name; this.author = author; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", author=" + author + "]"; } } BookController.java package org.fkit.controller; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.fkit.domain.Book; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import com.fasterxml.jackson.databind.ObjectMapper; @Controller @RequestMapping("/json") public class BookController { private static final Log logger = LogFactory.getLog(BookController.class); @RequestMapping(value="/testRequestBody") public void setJson(@RequestBody Book book, HttpServletResponse response) throws Exception { ObjectMapper mapper = new ObjectMapper(); logger.info(mapper.writeValueAsString(book)); book.setAuthor("chengang"); response.setContentType("text/html; charset=UTF-8"); response.getWriter().println(mapper.writeValueAsString(book)); } } index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <title>测试接收JSON格式数据</title> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> <script type="text/javascript" src="json2.js"></script> <script type="text/javascript"> $(document).ready(function() { testRequestBody(); }); function testRequestBody() { $.ajax("${pageContext.request.contextPath}/json/testRequestBody" , { dataType:"json", type:"post", contentType:"application/json", data:JSON.stringify({id:1, name: "Spring MVC"}), async: true, success: function(data) { console.log(data); $("#id").html(data.id); $("#name").html(data.name); $("#author").html(data.author); }, error: function() { alert("send data error!"); } }); } </script> </head> <body> <h2>测试接收JSON格式数据</h2> 编号:<span id="id"></span><br> 书名:<span id="name"></span><br> 作者:<span id="author"></span><br> </body> </html>

优秀的个人博客,低调大师

sklearn.feature_extraction.DictVectorizer将字典格式的数据转换为特征

class sklearn.feature_extraction. DictVectorizer ( dtype=<class ‘numpy.float64’> , separator=’=’ , sparse=True , sort=True ) Transforms lists of feature-value mappings to vectors. This transformer turns lists of mappings (dict-like objects ) of feature names to feature values into Numpy arrays or scipy. sparse matrices(稀疏矩阵) for use with scikit-learn estimators. When feature values are strings, this transformer will do a binary one-hot (aka one-of-K) coding: one boolean-valued feature is constructed for each of the possible string values that the feature can take on. For instance, a feature “f” that can take on the values “ham” and “spam” will become two features in the output, one signifying “f=ham”, the other “f=spam”. However, note that this transformer will only do a binary one-hot encoding when feature values are of type string. If categorical features are represented as numeric values such as int, the DictVectorizer can be followed by OneHotEncoder to complete binary one-hot encoding. Features that do not occur in a sample (mapping) will have a zero value in the resulting array/matrix. Read more in the User Guide . Parameters: dtype : callable, optional The type of feature values. Passed to Numpy array/scipy.sparse matrix constructors as the dtype argument. separator : string, optional Separator string used when constructing new features for one-hot coding. sparse : boolean, optional. Whether transform should produce scipy.sparse matrices. True by default. sort : boolean, optional. Whether feature_names_ and vocabulary_ should be sorted when fitting. True by default. Attributes: vocabulary_ : dict A dictionary mapping feature names to feature indices. feature_names_ : list A list of length n_features containing the feature names (e.g., “f=ham” and “f=spam”).

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册