Get the distance between two geo points

后端 未结 9 560
旧巷少年郎
旧巷少年郎 2020-11-27 09:11

I want to make an app which checks the nearest place where a user is. I can easily get the location of the user and I have already a list of places with latitude and longitu

相关标签:
9条回答
  • 2020-11-27 09:46

    a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)

    c = 2 ⋅ atan2( √a, √(1−a) )

    distance = R ⋅ c

    where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);

    note that angles need to be in radians to pass to trig functions!

    fun distanceInMeter(firstLocation: Location, secondLocation: Location): Double {
        val earthRadius = 6371000.0
        val deltaLatitudeDegree = (firstLocation.latitude - secondLocation.latitude) * Math.PI / 180f
        val deltaLongitudeDegree = (firstLocation.longitude - secondLocation.longitude) * Math.PI / 180f
        val a = sin(deltaLatitudeDegree / 2).pow(2) +
                cos(firstLocation.latitude * Math.PI / 180f) * cos(secondLocation.latitude * Math.PI / 180f) *
                sin(deltaLongitudeDegree / 2).pow(2)
        val c = 2f * atan2(sqrt(a), sqrt(1 - a))
        return earthRadius * c
    }
    
    
    data class Location(val latitude: Double, val longitude: Double)
    
    0 讨论(0)
  • 2020-11-27 09:51

    There are two ways to get distance between LatLng.

    public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
    

    See this

    and second

    public float distanceTo (Location dest) as answered by praveen.

    0 讨论(0)
  • 2020-11-27 09:53
    private float getDistance(double lat1, double lon1, double lat2, double lon2) {
            float[] distance = new float[2];
            Location.distanceBetween(lat1, lon1, lat2, lon2, distance);
            return distance[0];
        }
    
    0 讨论(0)
  • 2020-11-27 09:56

    There are a couple of methods you could use, but to determine which one is best we first need to know if you are aware of the user's altitude, as well as the altitude of the other points?

    Depending on the level of accuracy you are after, you could look into either the Haversine or Vincenty formulae...

    These pages detail the formulae, and, for the less mathematically inclined also provide an explanation of how to implement them in script!

    Haversine Formula: http://www.movable-type.co.uk/scripts/latlong.html

    Vincenty Formula: http://www.movable-type.co.uk/scripts/latlong-vincenty.html

    If you have any problems with any of the meanings in the formulae, just comment and I'll do my best to answer them :)

    0 讨论(0)
  • 2020-11-27 09:58

    you can get distance and time using google Map API Google Map API

    just pass downloaded JSON to this method you will get real time Distance and Time between two latlong's

    void parseJSONForDurationAndKMS(String json) throws JSONException {
    
        Log.d(TAG, "called parseJSONForDurationAndKMS");
        JSONObject jsonObject = new JSONObject(json);
        String distance;
        String duration;
        distance = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getString("text");
        duration = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("duration").getString("text");
    
        Log.d(TAG, "distance : " + distance);
        Log.d(TAG, "duration : " + duration);
    
        distanceBWLats.setText("Distance : " + distance + "\n" + "Duration : " + duration);
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 10:02

    An approximated solution (based on an equirectangular projection), much faster (it requires only 1 trig and 1 square root).

    This approximation is relevant if your points are not too far apart. It will always over-estimate compared to the real haversine distance. For example it will add no more than 0.05382 % to the real distance if the delta latitude or longitude between your two points does not exceed 4 decimal degrees.

    The standard formula (Haversine) is the exact one (that is, it works for any couple of longitude/latitude on earth) but is much slower as it needs 7 trigonometric and 2 square roots. If your couple of points are not too far apart, and absolute precision is not paramount, you can use this approximate version (Equirectangular), which is much faster as it uses only one trigonometric and one square root.

    // Approximate Equirectangular -- works if (lat1,lon1) ~ (lat2,lon2)
    int R = 6371; // km
    double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
    double y = (lat2 - lat1);
    double distance = Math.sqrt(x * x + y * y) * R;
    

    You can optimize this further by either:

    1. Removing the square root if you simply compare the distance to another (in that case compare both squared distance);
    2. Factoring-out the cosine if you compute the distance from one master point to many others (in that case you do the equirectangular projection centered on the master point, so you can compute the cosine once for all comparisons).

    For more info see: http://www.movable-type.co.uk/scripts/latlong.html

    There is a nice reference implementation of the Haversine formula in several languages at: http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

    0 讨论(0)
提交回复
热议问题