LocationManager and jumping coordinates

房东的猫 提交于 2019-11-29 08:59:45

That's pretty typical behavior from a smartphone/consumer-grade GPS. To reduce the fluctuations you'll need to normalize the results. One approach is to compute the geographic center from a queue of coordinates, for example: https://github.com/Esri/cordova-plugin-advanced-geolocation/blob/master/src/com/esri/cordova/geolocation/utils/GeodataHelper.java#L56

public class Coordinate {
    public double latitude;
    public double longitude;
    public float accuracy;
}

public static Coordinate getGeographicCenter(final ConcurrentLinkedQueue<Coordinate> queue){
    double x = 0;
    double y = 0;
    double z = 0;
    float accuracy = 0;
    int radiusKM = 6367;

    for(final Coordinate coordinate : queue){
        accuracy += coordinate.accuracy;

        // Convert latitude and longitude to radians
        final double latRad = Math.PI * coordinate.latitude / 180;
        final double lonRad = Math.PI * coordinate.longitude / 180;

        // Convert to cartesian coords
        x += radiusKM * Math.cos(latRad) * Math.cos(lonRad);
        y += radiusKM * Math.cos(latRad) * Math.sin(lonRad);
        z += radiusKM * Math.sin(latRad);
    }

    // Get our averages
    final double xAvg = x / queue.size();
    final double yAvg = y / queue.size();
    final double zAvg = z / queue.size();
    final float accuracyAvg = accuracy / queue.size();

    // Convert cartesian back to radians
    final double sphericalLatRads = Math.asin(zAvg / radiusKM);
    final double sphericalLonRads = Math.atan2(yAvg, xAvg);

    // Convert radians back to latitude and longitude
    Coordinate centerPoint = new Coordinate();
    centerPoint.latitude = sphericalLatRads * (180 / Math.PI);
    centerPoint.longitude = sphericalLonRads * (180 / Math.PI);
    centerPoint.accuracy = accuracyAvg;

    return centerPoint;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!