d. 启动定位获得用户当前所在位置(WGS-84坐标系),即可得到如下结果:
e. 创建Marker标记,标记Location的定位位置,可在LocationCallback回调中调用此方法 。
private Marker locationMarker;//添加定位位置标记public void addLocationMarker(double Latitude, double Longitude) { if (null != locationMarker) { locationMarker.remove(); } MarkerOptions options = new MarkerOptions() .position(new LatLng(Latitude, Longitude)) .title("定位位置") .snippet("定位所在位置"); locationMarker = this.huaweiMap.addMarker(options);}f. 从下方2图可以看出,Location定位获取的位置与用户实际所在位置存在较大偏差:

文章插图

文章插图
3. 将Location SDK获取的“WGS-84坐标系”的经纬度转换为“GCJ-02坐标系”的经纬度a. 实现WGS-84坐标系 转 GCJ-02坐标系的方法 。
public static double pi = 3.1415926535897932384626;public static double a = 6378245.0;public static double ee = 0.00669342162296594323;/** * WGS-84 转 GCJ-02 */public static double[] wgs84_To_Gcj02(double lat, double lon) { if (outOfChina(lat, lon)) { return new double[] { lat, lon }; } double dLat = transformLat(lon - 105.0, lat - 35.0); double dLon = transformLon(lon - 105.0, lat - 35.0); double radLat = lat / 180.0 * pi; double magic = Math.sin(radLat); magic = 1 - ee * magic * magic; double sqrtMagic = Math.sqrt(magic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi); double mgLat = lat + dLat; double mgLon = lon + dLon; return new double[] { mgLat, mgLon };}public static double transformLat(double x, double y) { double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0; ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0; return ret;}public static double transformLon(double x, double y) { double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0; return ret;}public static boolean outOfChina(double lat, double lon) { if (lon < 72.004 || lon > 137.8347) return true; if (lat < 0.8293 || lat > 55.8271) return true; return false;}
经验总结扩展阅读
- C# 8.0 添加和增强的功能【基础篇】
- 一家人温馨的句子简短 形容幸福满满一家句子
- 二氧化碳灭火器适用于什么灭火
- pet是食品级的吗
- 聊天什么程度算暧昧
- 驱动开发:内核LDE64引擎计算汇编长度
- 熟粽子冷冻6个月能吃吗
- 豆角属于碳水还是蔬菜
- 拍身份证黄头发怎么办
- 酒后睡了7个小时还算酒驾吗