文章插图
文章插图
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;}b. 调用上述方法,将Location SDK获取的经纬度的坐标系转换为GCJ-02,可在LocationCallback回调方法中执行转换代码 。
Log.d("LOG_HwMyLocation", "WGS-86坐标系经纬度:"+ locationResult.getLastHWLocation().getLatitude() + " ; "+ locationResult.getLastHWLocation().getLongitude());Log.d("LOG_HwMyLocation", "===============转换地理坐标系===============");double[] Gcj02LatLng = wgs84_To_Gcj02(locationResult.getLastHWLocation().getLatitude(),locationResult.getLastHWLocation().getLongitude());Log.d("LOG_HwMyLocation", "Gcj-02坐标系经纬度:" + Gcj02LatLng[0]+" ; "+Gcj02LatLng[1]+"\n");//将转换坐标系之后的经纬度 标记在地图上addGCJ02LocationMarker(Gcj02LatLng[0], Gcj02LatLng[1]);
推荐阅读
- C# 8.0 添加和增强的功能【基础篇】
- 我的世界怎么开二级隐身(我的世界怎么搞隐身)
- 华为mate50pro上市价格_华为mate50pro价格官网报价
- 【保姆教程】RuoYi-Radius搭建实现portal认证
- 原神证悟木怎么快速收集
- 鸡皮疙瘩第三关殖民星通关攻略
- QQ飞车结婚流程(qq飞车结婚有什么好处)
- qq飞车怎么结婚姐妹(qq飞车结婚有什么要求)
- 【日志系统】Loki日志监控 - 入门初体验
- Python3.11正式版,它来了!