LocationManager and jumping coordinates

前端 未结 1 1870
南旧
南旧 2020-12-18 16:00

Take a look at this image. You see that some how LocationManager gives weird coordinates. It looks like they jump. But at the same time native google map app display correc

相关标签:
1条回答
  • 2020-12-18 16:03

    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;
    }
    
    0 讨论(0)
提交回复
热议问题