Calculate distance between 2 GPS coordinates

前端 未结 29 3490
青春惊慌失措
青春惊慌失措 2020-11-21 23:34

How do I calculate distance between two GPS coordinates (using latitude and longitude)?

相关标签:
29条回答
  • 2020-11-21 23:55

    here is the Swift implementation from the answer

    func degreesToRadians(degrees: Double) -> Double {
        return degrees * Double.pi / 180
    }
    
    func distanceInKmBetweenEarthCoordinates(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {
    
        let earthRadiusKm: Double = 6371
    
        let dLat = degreesToRadians(degrees: lat2 - lat1)
        let dLon = degreesToRadians(degrees: lon2 - lon1)
    
        let lat1 = degreesToRadians(degrees: lat1)
        let lat2 = degreesToRadians(degrees: lat2)
    
        let a = sin(dLat/2) * sin(dLat/2) +
        sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat2)
        let c = 2 * atan2(sqrt(a), sqrt(1 - a))
        return earthRadiusKm * c
    }
    
    0 讨论(0)
  • 2020-11-21 23:57

    Calculate the distance between two coordinates by latitude and longitude, including a Javascript implementation.

    West and South locations are negative. Remember minutes and seconds are out of 60 so S31 30' is -31.50 degrees.

    Don't forget to convert degrees to radians. Many languages have this function. Or its a simple calculation: radians = degrees * PI / 180.

    function degreesToRadians(degrees) {
      return degrees * Math.PI / 180;
    }
    
    function distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) {
      var earthRadiusKm = 6371;
    
      var dLat = degreesToRadians(lat2-lat1);
      var dLon = degreesToRadians(lon2-lon1);
    
      lat1 = degreesToRadians(lat1);
      lat2 = degreesToRadians(lat2);
    
      var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
              Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      return earthRadiusKm * c;
    }
    

    Here are some examples of usage:

    distanceInKmBetweenEarthCoordinates(0,0,0,0)  // Distance between same 
                                                  // points should be 0
    0
    
    distanceInKmBetweenEarthCoordinates(51.5, 0, 38.8, -77.1) // From London
                                                              // to Arlington
    5918.185064088764
    
    0 讨论(0)
  • 2020-11-21 23:59

    Scala version

      def deg2rad(deg: Double) = deg * Math.PI / 180.0
    
      def rad2deg(rad: Double) = rad / Math.PI * 180.0
    
      def getDistanceMeters(lat1: Double, lon1: Double, lat2: Double, lon2: Double) = {
        val theta = lon1 - lon2
        val dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) *
          Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta))
        Math.abs(
          Math.round(
            rad2deg(Math.acos(dist)) * 60 * 1.1515 * 1.609344 * 1000)
        )
      }
    
    0 讨论(0)
  • 2020-11-22 00:03

    PHP version:

    (Remove all deg2rad() if your coordinates are already in radians.)

    $R = 6371; // km
    $dLat = deg2rad($lat2-$lat1);
    $dLon = deg2rad($lon2-$lon1);
    $lat1 = deg2rad($lat1);
    $lat2 = deg2rad($lat2);
    
    $a = sin($dLat/2) * sin($dLat/2) +
         sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2); 
    
    $c = 2 * atan2(sqrt($a), sqrt(1-$a)); 
    $d = $R * $c;
    
    0 讨论(0)
  • 2020-11-22 00:03

    Dart Version

    Haversine Algorithm.

    import 'dart:math';
    
    class GeoUtils {
    
      static double _degreesToRadians(degrees) {
        return degrees * pi / 180;
      }
    
      static double distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2) {
        var earthRadiusKm = 6371;
    
        var dLat = _degreesToRadians(lat2-lat1);
        var dLon = _degreesToRadians(lon2-lon1);
    
        lat1 = _degreesToRadians(lat1);
        lat2 = _degreesToRadians(lat2);
    
        var a = sin(dLat/2) * sin(dLat/2) +
            sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat2);
        var c = 2 * atan2(sqrt(a), sqrt(1-a));
        return earthRadiusKm * c;
      }
    }
    
    0 讨论(0)
  • 2020-11-22 00:05

    It depends on how accurate you need it to be, if you need pinpoint accuracy, is best to look at an algorithm with uses an ellipsoid, rather than a sphere, such as Vincenty's algorithm, which is accurate to the mm. http://en.wikipedia.org/wiki/Vincenty%27s_algorithm

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