本文主要是介绍Android通过地址反查经纬度——原生位置信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android通过地址反查经纬度——原生位置信息
通过地址反查经纬度,得到的经纬度是基于火星坐标系GCJ02LL的经纬度,如果使用百度定位sdk,则需要转化为bd09LL的坐标系。
Geocoder gc = new Geocoder(MapFragment.this.getActivity(), Locale.CHINA);
List<Address> addresseList = null;
try {addresseList = gc.getFromLocationName(voiceQueryText, 1);if(addresseList!=null&&addresseList.size()>0){Address address_temp = result.get(0);double lat = address_temp.getLatitude();double lon = address_temp.getLongitude();//火星坐标系转化为百度坐标系CoordGCJ02LL2BDLL gcj02ll2bdll = new CoordGCJ02LL2BDLL();double coord[] = gcj02ll2bdll.convert(lon, lat);Log.d("经纬度","lon="+coord[0]+";lat="+coord[1]);}
} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();
}
火星坐标系转化为百度坐标系
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CoordGCJ02LL2BDLL{public CoordGCJ02LL2BDLL(){//cn/com/XXX/XXX/XXX/此为CoordGCJ02LL2BDLL所在包名logger = LogFactory.getLog(cn/com/XXX/XXX/XXX/CoordGCJ02LL2BDLL);}public double[] convert(double gcjlon, double gcjlat){try{double bdlonlat[] = gcj2bd(gcjlat, gcjlon);logger.debug((new StringBuilder()).append("GCJ02\u7ECF\u7EAC\u5EA6\u8F6C\u767E\u5EA6\u7ECF\u7EAC\u5EA6\uFF0C\u8F93\u5165\uFF1Agcj02lon=").append(gcjlon).append(" ,gcj02lat=").append(gcjlat).append(",\u7ED3\u679C\uFF1ABaiduLon=").append(bdlonlat[1]).append(",BaiduLat=").append(bdlonlat[0]).toString());return (new double[] {bdlonlat[1], bdlonlat[0]});}catch(Exception e){return (new double[] {-1D, -1D});}}public static double[] gcj2bd(double lat, double lon){double x = lon;double y = lat;double z = Math.sqrt(x * x + y * y) + 2.0000000000000002E-005D * Math.sin(y * x_pi);double theta = Math.atan2(y, x) + 3.0000000000000001E-006D * Math.cos(x * x_pi);double bd_lon = z * Math.cos(theta) + 0.0064999999999999997D;double bd_lat = z * Math.sin(theta) + 0.0060000000000000001D;return (new double[] {bd_lat, bd_lon});}
}
这篇关于Android通过地址反查经纬度——原生位置信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!