前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自学HarmonyOS应用开发(54)- 校正定位偏差

自学HarmonyOS应用开发(54)- 校正定位偏差

作者头像
面向对象思考
发布2021-07-15 16:48:47
3040
发布2021-07-15 16:48:47
举报
上一篇文章已经介绍了如果获取当前所在位置的方法,这种方法存在一个问题:和实际位置之前存在500米左右的偏差。

原因调查

经过一番调查,结论是gps信号使用的是WGS-84坐标系,而高德地图使用的是GCJ-02火星坐标系,只有经过坐标变换才能显示正确的位置。这方面的文章网上有很多,这里采用以下博客文章中的代码:

代码语言:javascript
复制
https://www.cnblogs.com/blogger-Li/p/11616835.html

代码如下:

代码语言:javascript
复制
// 本代码引用自Hugo_nice下面的博客文章
// https://www.cnblogs.com/blogger-Li/p/11616835.html
package xwg.harmony.stopwatch;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * gps纠偏算法,适用于google,高德体系的地图
 */
public abstract class GpsUtil {

    private final static double a = 6378245.0;
    private final static double pi = 3.1415926535897932384626;
    private final static double ee = 0.00669342162296594323;

    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param lat1
     *            第一点纬度
     * @param lng1
     *            第一点经度
     * @param lat2
     *            第二点纬度
     * @param lng2
     *            第二点经度
     * @return 返回距离 单位:米
     */
    public static double distance(double lat1, double lng1, double lat2, double lng2) {
        double a, b, R;
        R = 6378137; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (lng1 - lng2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
        return d;
    }

    /**
     * Description: WGS-84 to GCJ-02 <BR>     *
     * @author dsn
     * @date 2017年10月24日 下午2:09:27
     * @param latitude
     *            纬度
     * @param longitude
     *            经度
     * @return [纬度,经度]
     * @version 1.0
     */
    public static double[] toGCJ02Point(double latitude, double longitude) {
        double[] dev = calDev(latitude, longitude);
        double retLat = latitude + dev[0];
        double retLon = longitude + dev[1];
        return new double[] { retLat, retLon };
    }

    /**
     * Description: WGS-84 to GCJ-02 <BR>     *
     * @author dsn
     * @date 2017年10月24日 下午2:09:27
     * @param latitude
     *            纬度
     * @param longitude
     *            经度
     * @param scale
     *            经纬度保留小数位数
     * @return [纬度,经度]
     * @version 1.0
     */
    public static double[] toGCJ02Point(double latitude, double longitude, int scale) {
        double[] dev = calDev(latitude, longitude);
        double retLat = latitude + dev[0];
        double retLon = longitude + dev[1];
        return new double[] { new BigDecimal(retLat).setScale(scale, RoundingMode.DOWN).doubleValue(),
                new BigDecimal(retLon).setScale(scale, RoundingMode.DOWN).doubleValue() };
    }

    /**
     * Description:GCJ-02 to WGS-84 <BR>     *
     * @author dsn
     * @date 2017年10月24日 下午2:09:54
     * @param latitude
     *            纬度
     * @param longitude
     *            经度
     * @return [纬度,经度]
     * @version 1.0
     */
    public static double[] toWGS84Point(double latitude, double longitude) {
        double[] dev = calDev(latitude, longitude);
        double retLat = latitude - dev[0];
        double retLon = longitude - dev[1];
        dev = calDev(retLat, retLon);
        retLat = latitude - dev[0];
        retLon = longitude - dev[1];
        return new double[] { retLat, retLon };
    }

    private static double[] calDev(double wgLat, double wgLon) {
        if (isOutOfChina(wgLat, wgLon)) {
            return new double[] { 0, 0 };
        }
        double dLat = calLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = calLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 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);
        return new double[] { dLat, dLon };
    }

    private static boolean isOutOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    private static double calLat(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;
    }

    private static double calLon(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;
    }
}

偏移调整

代码语言:javascript
复制
public void setLocation(double long_deg, double lat_deg){
    double ret[] = GpsUtil.toGCJ02Point(lat_deg, long_deg);
    latitude = ret[0];
    longitude = ret[1];
    invalidate();
}

在设定经纬度之前,调用转换方法即可。经过转换之后的位置和实际位置之间的偏差可以在几十米范围之内。

参考代码

完整代码可以从以下链接下载:

https://github.com/xueweiguo/Harmony/tree/master/StopWatch

参考资料

GCJ-02火星坐标系和WGS-84坐标系转换关系:

代码语言:javascript
复制
https://www.cnblogs.com/blogger-Li/p/11616835.html

获取设备的位置信息:

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/device-location-info-0000000000031900

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档