Frage

Wie berechne ich den Abstand zwischen zwei durch Breiten- und Längengrad angegebenen Punkten?

Zur Verdeutlichung hätte ich gerne die Entfernung in Kilometern;Die Punkte verwenden das WGS84-System und ich würde gerne die relative Genauigkeit der verfügbaren Ansätze verstehen.

War es hilfreich?

Lösung

Das Verknüpfung könnte für Sie hilfreich sein, da es die Verwendung des detailliert beschreibt Haversin-Formel um die Entfernung zu berechnen.

Auszug:

Dieses Skript [in JavaScript] berechnet Großkreisstrecken zwischen den beiden Punkten-dh der kürzesten Entfernung über der Erdoberfläche-unter Verwendung der Formel "Haversine".

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

Andere Tipps

Ich musste für mein Projekt viele Abstände zwischen den Punkten berechnen, also habe ich versucht, den Code zu optimieren, den ich hier gefunden habe.Im Durchschnitt in verschiedenen Browsern meine neue Implementierung läuft 2 mal schneller als die am meisten positiv bewertete Antwort.

function distance(lat1, lon1, lat2, lon2) {
  var p = 0.017453292519943295;    // Math.PI / 180
  var c = Math.cos;
  var a = 0.5 - c((lat2 - lat1) * p)/2 + 
          c(lat1 * p) * c(lat2 * p) * 
          (1 - c((lon2 - lon1) * p))/2;

  return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
}

Sie können mit meinem jsPerf spielen und sehen Ergebnisse hier.

Kürzlich musste ich dasselbe in Python tun, also hier ist eine Python-Implementierung:

from math import cos, asin, sqrt
def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295     #Pi/180
    a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
    return 12742 * asin(sqrt(a)) #2*R*asin...

Und der Vollständigkeit halber: Haversine auf Wiki.

Hier ist eine C#-Implementierung:

static class DistanceAlgorithm
{
    const double PIx = 3.141592653589793;
    const double RADIUS = 6378.16;

    /// <summary>
    /// Convert degrees to Radians
    /// </summary>
    /// <param name="x">Degrees</param>
    /// <returns>The equivalent in radians</returns>
    public static double Radians(double x)
    {
        return x * PIx / 180;
    }

    /// <summary>
    /// Calculate the distance between two places.
    /// </summary>
    /// <param name="lon1"></param>
    /// <param name="lat1"></param>
    /// <param name="lon2"></param>
    /// <param name="lat2"></param>
    /// <returns></returns>
    public static double DistanceBetweenPlaces(
        double lon1,
        double lat1,
        double lon2,
        double lat2)
    {
        double dlon = Radians(lon2 - lon1);
        double dlat = Radians(lat2 - lat1);

        double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
        double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        return angle * RADIUS;
    }

}

Hier ist eine Java-Implementierung der Haversine-Formel.

public final static double AVERAGE_RADIUS_OF_EARTH_KM = 6371;
public int calculateDistanceInKilometer(double userLat, double userLng,
  double venueLat, double venueLng) {

    double latDistance = Math.toRadians(userLat - venueLat);
    double lngDistance = Math.toRadians(userLng - venueLng);

    double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
      + Math.cos(Math.toRadians(userLat)) * Math.cos(Math.toRadians(venueLat))
      * Math.sin(lngDistance / 2) * Math.sin(lngDistance / 2);

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return (int) (Math.round(AVERAGE_RADIUS_OF_EARTH_KM * c));
}

Beachten Sie, dass wir hier die Antwort auf den nächsten Kilometer runden.

Vielen Dank für all das.Ich habe den folgenden Code in meiner Objective-C-iPhone-App verwendet:

const double PIx = 3.141592653589793;
const double RADIO = 6371; // Mean radius of Earth in Km

double convertToRadians(double val) {

   return val * PIx / 180;
}

-(double)kilometresBetweenPlace1:(CLLocationCoordinate2D) place1 andPlace2:(CLLocationCoordinate2D) place2 {

        double dlon = convertToRadians(place2.longitude - place1.longitude);
        double dlat = convertToRadians(place2.latitude - place1.latitude);

        double a = ( pow(sin(dlat / 2), 2) + cos(convertToRadians(place1.latitude))) * cos(convertToRadians(place2.latitude)) * pow(sin(dlon / 2), 2);
        double angle = 2 * asin(sqrt(a));

        return angle * RADIO;
}

Breiten- und Längengrad werden im Dezimalformat angegeben.Ich habe min() nicht für den asin()-Aufruf verwendet, da die Entfernungen, die ich verwende, so klein sind, dass sie nicht erforderlich sind.

Es gab falsche Antworten, bis ich die Werte im Bogenmaß eingegeben habe – jetzt sind es im Wesentlichen die gleichen wie die Werte, die ich von Apples Karten-App erhalten habe :-)

Zusätzliches Update:

Wenn Sie iOS4 oder höher verwenden, bietet Apple hierfür einige Methoden an, sodass die gleiche Funktionalität erreicht wird mit:

-(double)kilometresBetweenPlace1:(CLLocationCoordinate2D) place1 andPlace2:(CLLocationCoordinate2D) place2 {

    MKMapPoint  start, finish;


    start = MKMapPointForCoordinate(place1);
    finish = MKMapPointForCoordinate(place2);

    return MKMetersBetweenMapPoints(start, finish) / 1000;
}

Dies ist eine einfache PHP-Funktion, die eine sehr vernünftige Näherung liefert (unter +/-1 % Fehlermarge).

<?php
function distance($lat1, $lon1, $lat2, $lon2) {

    $pi80 = M_PI / 180;
    $lat1 *= $pi80;
    $lon1 *= $pi80;
    $lat2 *= $pi80;
    $lon2 *= $pi80;

    $r = 6372.797; // mean radius of Earth in km
    $dlat = $lat2 - $lat1;
    $dlon = $lon2 - $lon1;
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlon / 2) * sin($dlon / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $km = $r * $c;

    //echo '<br/>'.$km;
    return $km;
}
?>

Wie bereits gesagt;Die Erde ist KEINE Kugel.Es ist wie ein alter, alter Baseball, mit dem Mark McGwire trainieren wollte – er ist voller Dellen und Beulen.Die einfacheren Berechnungen (wie diese) behandeln es wie eine Kugel.

Verschiedene Methoden können mehr oder weniger präzise sein, je nachdem, wo Sie sich auf diesem unregelmäßigen Ei befinden UND wie weit Ihre Punkte voneinander entfernt sind (je näher sie beieinander liegen, desto kleiner ist die absolute Fehlerspanne).Je genauer Ihre Erwartung, desto komplexer ist die Mathematik.

Für mehr Information: Wikipedia geografische Entfernung

Ich poste hier mein Arbeitsbeispiel.

Listen Sie alle Punkte in der Tabelle auf, deren Abstand zwischen einem bestimmten Punkt (wir verwenden einen zufälligen Punkt – Breitengrad: 45,20327, Längengrad: 23,7806) weniger als 50 km beträgt, mit Breiten- und Längengrad in MySQL (die Tabellenfelder sind coord_lat und coord_long):

Listen Sie alle mit DISTANCE<50 auf, in Kilometern (berücksichtigter Erdradius 6371 KM):

SELECT denumire, (6371 * acos( cos( radians(45.20327) ) * cos( radians( coord_lat ) ) * cos( radians( 23.7806 ) - radians(coord_long) ) + sin( radians(45.20327) ) * sin( radians(coord_lat) ) )) AS distanta 
FROM obiective 
WHERE coord_lat<>'' 
    AND coord_long<>'' 
HAVING distanta<50 
ORDER BY distanta desc

Das obige Beispiel wurde in MySQL 5.0.95 und 5.5.16 (Linux) getestet.

In den anderen Antworten eine Implementierung in wird vermisst.

Die Berechnung des Abstands zwischen zwei Punkten ist mit dem ganz einfach distm Funktion aus dem geosphere Paket:

distm(p1, p2, fun = distHaversine)

Wo:

p1 = longitude/latitude for point(s)
p2 = longitude/latitude for point(s)
# type of distance calculation
fun = distCosine / distHaversine / distVincentySphere / distVincentyEllipsoid 

Da die Erde nicht perfekt kugelförmig ist, ist die Vincenty-Formel für Ellipsoide ist wahrscheinlich die beste Möglichkeit, Entfernungen zu berechnen.So in der geosphere Paket, das Sie dann verwenden:

distm(p1, p2, fun = distVincentyEllipsoid)

Natürlich muss man es nicht unbedingt verwenden geosphere Paket können Sie den Abstand auch in Basis berechnen R mit einer Funktion:

hav.dist <- function(long1, lat1, long2, lat2) {
  R <- 6371
  diff.long <- (long2 - long1)
  diff.lat <- (lat2 - lat1)
  a <- sin(diff.lat/2)^2 + cos(lat1) * cos(lat2) * sin(diff.long/2)^2
  b <- 2 * asin(pmin(1, sqrt(a))) 
  d = R * b
  return(d)
}

Die Haversine ist in den meisten Fällen definitiv eine gute Formel. Andere Antworten enthalten sie bereits, daher werde ich den Platz nicht einnehmen.Es ist jedoch wichtig zu beachten, dass es keine Rolle spielt, welche Formel verwendet wird (ja, nicht nur eine).Aufgrund des enormen Genauigkeitsbereichs und der erforderlichen Rechenzeit.Die Wahl der Formel erfordert etwas mehr Überlegung als eine einfache Antwort.

Dieser Beitrag von einer Person bei der NASA ist der beste, den ich zur Diskussion der Optionen gefunden habe

http://www.cs.nyu.edu/visual/home/proj/tiger/gisfaq.html

Wenn Sie beispielsweise Zeilen nur nach Entfernung in einem 100-Meilen-Radius sortieren.Die Flat-Earth-Formel wird viel schneller sein als die Haversine.

HalfPi = 1.5707963;
R = 3956; /* the radius gives you the measurement unit*/

a = HalfPi - latoriginrad;
b = HalfPi - latdestrad;
u = a * a + b * b;
v = - 2 * a * b * cos(longdestrad - longoriginrad);
c = sqrt(abs(u + v));
return R * c;

Beachten Sie, dass es nur einen Kosinus und eine Quadratwurzel gibt.Vs. 9 davon auf der Haversine-Formel.

Sie können den Build in CLLocationDistance verwenden, um dies zu berechnen:

CLLocation *location1 = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:latitude2 longitude:longitude2];
[self distanceInMetersFromLocation:location1 toLocation:location2]

- (int)distanceInMetersFromLocation:(CLLocation*)location1 toLocation:(CLLocation*)location2 {
    CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];
    return distanceInMeters;
}

Wenn Sie in Ihrem Fall Kilometer möchten, dividieren Sie einfach durch 1000.

Ich möchte nicht noch eine weitere Antwort hinzufügen, aber die Google Maps API v.3 verfügt über sphärische Geometrie (und mehr).Nachdem Sie Ihren WGS84 in Dezimalgrad umgerechnet haben, können Sie Folgendes tun:

<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry" type="text/javascript"></script>  

distance = google.maps.geometry.spherical.computeDistanceBetween(
    new google.maps.LatLng(fromLat, fromLng), 
    new google.maps.LatLng(toLat, toLng));

Kein Wort darüber, wie genau die Berechnungen von Google sind oder welches Modell verwendet wird (obwohl dort „sphärisch“ statt „geoid“ steht).Übrigens unterscheidet sich die „Luftlinie“-Entfernung offensichtlich von der Entfernung, die man auf der Erdoberfläche zurücklegt, was offenbar jeder annimmt.

Python Implimentation Origin ist das Zentrum der zusammenhängenden Vereinigten Staaten.

from haversine import haversine
origin = (39.50, 98.35)
paris = (48.8567, 2.3508)
haversine(origin, paris, miles=True)

Um die Antwort in Kilometern zu erhalten, setzen Sie einfach miles=false.

Es könnte eine einfachere und korrektere Lösung geben:Der Umfang der Erde beträgt am Äquator 40.000 km, etwa 37.000 km im Greenwich-Zyklus (oder einem anderen Längengrad).Daher:

pythagoras = function (lat1, lon1, lat2, lon2) {
   function sqr(x) {return x * x;}
   function cosDeg(x) {return Math.cos(x * Math.PI / 180.0);}

   var earthCyclePerimeter = 40000000.0 * cosDeg((lat1 + lat2) / 2.0);
   var dx = (lon1 - lon2) * earthCyclePerimeter / 360.0;
   var dy = 37000000.0 * (lat1 - lat2) / 360.0;

   return Math.sqrt(sqr(dx) + sqr(dy));
};

Ich stimme zu, dass eine Feinabstimmung erfolgen sollte, da ich selbst gesagt habe, dass es sich um ein Ellipsoid handelt, sodass der mit dem Kosinus zu multiplizierende Radius variiert.Aber es ist etwas genauer.Im Vergleich zu Google Maps wurde der Fehler deutlich reduziert.

Bei allen obigen Antworten wird davon ausgegangen, dass die Erde eine Kugel ist.Eine genauere Näherung wäre jedoch die eines abgeflachten Sphäroids.

a= 6378.137#equitorial radius in km
b= 6356.752#polar radius in km

def Distance(lat1, lons1, lat2, lons2):
    lat1=math.radians(lat1)
    lons1=math.radians(lons1)
    R1=(((((a**2)*math.cos(lat1))**2)+(((b**2)*math.sin(lat1))**2))/((a*math.cos(lat1))**2+(b*math.sin(lat1))**2))**0.5 #radius of earth at lat1
    x1=R*math.cos(lat1)*math.cos(lons1)
    y1=R*math.cos(lat1)*math.sin(lons1)
    z1=R*math.sin(lat1)

    lat2=math.radians(lat2)
    lons2=math.radians(lons2)
    R1=(((((a**2)*math.cos(lat2))**2)+(((b**2)*math.sin(lat2))**2))/((a*math.cos(lat2))**2+(b*math.sin(lat2))**2))**0.5 #radius of earth at lat2
    x2=R*math.cos(lat2)*math.cos(lons2)
    y2=R*math.cos(lat2)*math.sin(lons2)
    z2=R*math.sin(lat2)

    return ((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5

Hier ist ein Typoskript Umsetzung der Haversine-Formel

static getDistanceFromLatLonInKm(lat1: number, lon1: number, lat2: number, lon2: number): number {
    var deg2Rad = deg => {
        return deg * Math.PI / 180;
    }

    var r = 6371; // Radius of the earth in km
    var dLat = deg2Rad(lat2 - lat1);   
    var dLon = deg2Rad(lon2 - lon1);
    var a =
        Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(deg2Rad(lat1)) * Math.cos(deg2Rad(lat2)) *
        Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = r * c; // Distance in km
    return d;
}

Dieses Skript [in PHP] berechnet Abstände zwischen den beiden Punkten.

public static function getDistanceOfTwoPoints($source, $dest, $unit='K') {
        $lat1 = $source[0];
        $lon1 = $source[1];
        $lat2 = $dest[0];
        $lon2 = $dest[1];

        $theta = $lon1 - $lon2;
        $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
        $dist = acos($dist);
        $dist = rad2deg($dist);
        $miles = $dist * 60 * 1.1515;
        $unit = strtoupper($unit);

        if ($unit == "K") {
            return ($miles * 1.609344);
        }
        else if ($unit == "M")
        {
            return ($miles * 1.609344 * 1000);
        }
        else if ($unit == "N") {
            return ($miles * 0.8684);
        } 
        else {
            return $miles;
        }
    }

Hier ist die SQL-Implementierung zur Berechnung der Entfernung in km:

SELECT UserId, ( 3959 * acos( cos( radians( your latitude here ) ) * cos( radians(latitude) ) * 
cos( radians(longitude) - radians( your longitude here ) ) + sin( radians( your latitude here ) ) * 
sin( radians(latitude) ) ) ) AS distance FROM user HAVING
distance < 5  ORDER BY distance LIMIT 0 , 5;

Um den Abstand zwischen zwei Punkten auf einer Kugel zu berechnen, müssen Sie Folgendes tun Großkreisberechnung.

Es gibt eine Reihe von C/C++-Bibliotheken, die bei der Kartenprojektion helfen MapTools wenn Sie Ihre Abstände auf eine ebene Fläche projizieren müssen.Dazu benötigen Sie die Projektionszeichenfolge der verschiedenen Koordinatensysteme.

Möglicherweise finden Sie auch MapWindow ein nützliches Werkzeug zur Visualisierung der Punkte.Da es sich um Open Source handelt, ist es außerdem eine nützliche Anleitung zur Verwendung der proj.dll-Bibliothek, die offenbar die zentrale Open-Source-Projektionsbibliothek ist.

Hier ist die akzeptierte Antwortimplementierung, die auf Java portiert wurde, falls jemand sie benötigt.

package com.project529.garage.util;


/**
 * Mean radius.
 */
private static double EARTH_RADIUS = 6371;

/**
 * Returns the distance between two sets of latitudes and longitudes in meters.
 * <p/>
 * Based from the following JavaScript SO answer:
 * http://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula,
 * which is based on https://en.wikipedia.org/wiki/Haversine_formula (error rate: ~0.55%).
 */
public double getDistanceBetween(double lat1, double lon1, double lat2, double lon2) {
    double dLat = toRadians(lat2 - lat1);
    double dLon = toRadians(lon2 - lon1);

    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
                    Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = EARTH_RADIUS * c;

    return d;
}

public double toRadians(double degrees) {
    return degrees * (Math.PI / 180);
}

Wie bereits erwähnt, sollte bei einer genauen Berechnung berücksichtigt werden, dass die Erde keine perfekte Kugel ist.Hier einige Vergleiche der verschiedenen hier angebotenen Algorithmen:

geoDistance(50,5,58,3)
Haversine: 899 km
Maymenn: 833 km
Keerthana: 897 km
google.maps.geometry.spherical.computeDistanceBetween(): 900 km

geoDistance(50,5,-58,-3)
Haversine: 12030 km
Maymenn: 11135 km
Keerthana: 10310 km
google.maps.geometry.spherical.computeDistanceBetween(): 12044 km

geoDistance(.05,.005,.058,.003)
Haversine: 0.9169 km
Maymenn: 0.851723 km
Keerthana: 0.917964 km
google.maps.geometry.spherical.computeDistanceBetween(): 0.917964 km

geoDistance(.05,80,.058,80.3)
Haversine: 33.37 km
Maymenn: 33.34 km
Keerthana: 33.40767 km
google.maps.geometry.spherical.computeDistanceBetween(): 33.40770 km

Auf kleinen Entfernungen scheint der Algorithmus von Keerthana mit dem von Google Maps übereinzustimmen.Google Maps scheint keinem einfachen Algorithmus zu folgen, was darauf hindeutet, dass es sich hier möglicherweise um die genaueste Methode handelt.

Wie dem auch sei, hier ist eine Javascript-Implementierung des Keerthana-Algorithmus:

function geoDistance(lat1, lng1, lat2, lng2){
    const a = 6378.137; // equitorial radius in km
    const b = 6356.752; // polar radius in km

    var sq = x => (x*x);
    var sqr = x => Math.sqrt(x);
    var cos = x => Math.cos(x);
    var sin = x => Math.sin(x);
    var radius = lat => sqr((sq(a*a*cos(lat))+sq(b*b*sin(lat)))/(sq(a*cos(lat))+sq(b*sin(lat))));

    lat1 = lat1 * Math.PI / 180;
    lng1 = lng1 * Math.PI / 180;
    lat2 = lat2 * Math.PI / 180;
    lng2 = lng2 * Math.PI / 180;

    var R1 = radius(lat1);
    var x1 = R1*cos(lat1)*cos(lng1);
    var y1 = R1*cos(lat1)*sin(lng1);
    var z1 = R1*sin(lat1);

    var R2 = radius(lat2);
    var x2 = R2*cos(lat2)*cos(lng2);
    var y2 = R2*cos(lat2)*sin(lng2);
    var z2 = R2*sin(lat2);

    return sqr(sq(x1-x2)+sq(y1-y2)+sq(z1-z2));
}

Hier ist die Implementierung von VB.NET. Diese Implementierung liefert Ihnen das Ergebnis in KM oder Meilen basierend auf einem Enum-Wert, den Sie übergeben.

Public Enum DistanceType
    Miles
    KiloMeters
End Enum

Public Structure Position
    Public Latitude As Double
    Public Longitude As Double
End Structure

Public Class Haversine

    Public Function Distance(Pos1 As Position,
                             Pos2 As Position,
                             DistType As DistanceType) As Double

        Dim R As Double = If((DistType = DistanceType.Miles), 3960, 6371)

        Dim dLat As Double = Me.toRadian(Pos2.Latitude - Pos1.Latitude)

        Dim dLon As Double = Me.toRadian(Pos2.Longitude - Pos1.Longitude)

        Dim a As Double = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(Me.toRadian(Pos1.Latitude)) * Math.Cos(Me.toRadian(Pos2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2)

        Dim c As Double = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)))

        Dim result As Double = R * c

        Return result

    End Function

    Private Function toRadian(val As Double) As Double

        Return (Math.PI / 180) * val

    End Function

End Class

Ich habe die Berechnung verkürzt, indem ich die Formel vereinfacht habe.

Hier ist es in Ruby:

include Math
earth_radius_mi = 3959
radians = lambda { |deg| deg * PI / 180 }
coord_radians = lambda { |c| { :lat => radians[c[:lat]], :lng => radians[c[:lng]] } }

# from/to = { :lat => (latitude_in_degrees), :lng => (longitude_in_degrees) }
def haversine_distance(from, to)
  from, to = coord_radians[from], coord_radians[to]
  cosines_product = cos(to[:lat]) * cos(from[:lat]) * cos(from[:lng] - to[:lng])
  sines_product = sin(to[:lat]) * sin(from[:lat])
  return earth_radius_mi * acos(cosines_product + sines_product)
end
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2,units) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; 
  var miles = d / 1.609344; 

if ( units == 'km' ) {  
return d; 
 } else {
return miles;
}}

Chucks Lösung, auch meilenweit gültig.

Hier ist meine Java-Implementierung zur Berechnung des Abstands über Dezimalgrade nach einiger Suche.Ich habe den mittleren Weltradius (aus Wikipedia) in km verwendet.Wenn Sie Meilen als Ergebnis erhalten möchten, verwenden Sie den Weltradius in Meilen.

public static double distanceLatLong2(double lat1, double lng1, double lat2, double lng2) 
{
  double earthRadius = 6371.0d; // KM: use mile here if you want mile result

  double dLat = toRadian(lat2 - lat1);
  double dLng = toRadian(lng2 - lng1);

  double a = Math.pow(Math.sin(dLat/2), 2)  + 
          Math.cos(toRadian(lat1)) * Math.cos(toRadian(lat2)) * 
          Math.pow(Math.sin(dLng/2), 2);

  double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

  return earthRadius * c; // returns result kilometers
}

public static double toRadian(double degrees) 
{
  return (degrees * Math.PI) / 180.0d;
}

Verwenden Sie in MySQL die folgende Funktion und übergeben Sie die Parameter als „using“. POINT(LONG,LAT)

CREATE FUNCTION `distance`(a POINT, b POINT)
 RETURNS double
    DETERMINISTIC
BEGIN

RETURN

GLength( LineString(( PointFromWKB(a)), (PointFromWKB(b)))) * 100000; -- To Make the distance in meters

END;
function getDistanceFromLatLonInKm(position1, position2) {
    "use strict";
    var deg2rad = function (deg) { return deg * (Math.PI / 180); },
        R = 6371,
        dLat = deg2rad(position2.lat - position1.lat),
        dLng = deg2rad(position2.lng - position1.lng),
        a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(deg2rad(position1.lat))
            * Math.cos(deg2rad(position1.lat))
            * Math.sin(dLng / 2) * Math.sin(dLng / 2),
        c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return R * c;
}

console.log(getDistanceFromLatLonInKm(
    {lat: 48.7931459, lng: 1.9483572},
    {lat: 48.827167, lng: 2.2459745}
));

Hier ist ein Beispiel in postgres sql (in km, für die Meilenversion ersetzen Sie 1,609344 durch die Version 0,8684)

CREATE OR REPLACE FUNCTION public.geodistance(alat float, alng float, blat  

float, blng  float)
  RETURNS float AS
$BODY$
DECLARE
    v_distance float;
BEGIN

    v_distance = asin( sqrt(
            sin(radians(blat-alat)/2)^2 
                + (
                    (sin(radians(blng-alng)/2)^2) *
                    cos(radians(alat)) *
                    cos(radians(blat))
                )
          )
        ) * cast('7926.3352' as float) * cast('1.609344' as float) ;


    RETURN v_distance;
END 
$BODY$
language plpgsql VOLATILE SECURITY DEFINER;
alter function geodistance(alat float, alng float, blat float, blng float)
owner to postgres;

Hier gibt es ein gutes Beispiel für die Entfernungsberechnung mit PHP http://www.geodatasource.com/developers/php :

 function distance($lat1, $lon1, $lat2, $lon2, $unit) {

     $theta = $lon1 - $lon2;
     $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
     $dist = acos($dist);
     $dist = rad2deg($dist);
     $miles = $dist * 60 * 1.1515;
     $unit = strtoupper($unit);

     if ($unit == "K") {
         return ($miles * 1.609344);
     } else if ($unit == "N") {
          return ($miles * 0.8684);
     } else {
          return $miles;
     }
 }

Hatte ein Problem mit math.deg in LUA ...Wenn jemand eine Lösung kennt, bereinigen Sie bitte diesen Code!

In der Zwischenzeit finden Sie hier eine Implementierung von Haversine in LUA (verwenden Sie diese mit Redis!)

function calcDist(lat1, lon1, lat2, lon2)
    lat1= lat1*0.0174532925
    lat2= lat2*0.0174532925
    lon1= lon1*0.0174532925
    lon2= lon2*0.0174532925

    dlon = lon2-lon1
    dlat = lat2-lat1

    a = math.pow(math.sin(dlat/2),2) + math.cos(lat1) * math.cos(lat2) * math.pow(math.sin(dlon/2),2)
    c = 2 * math.asin(math.sqrt(a))
    dist = 6371 * c      -- multiply by 0.621371 to convert to miles
    return dist
end

Prost!

Hier ist eine weitere Konvertierung Rubin Code:

include Math
#Note: from/to = [lat, long]

def get_distance_in_km(from, to)
  radians = lambda { |deg| deg * Math.PI / 180 }
  radius = 6371 # Radius of the earth in kilometer
  dLat = radians[to[0]-from[0]]
  dLon = radians[to[1]-from[1]]

  cosines_product = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(radians[from[0]]) * Math.cos(radians[to[1]]) * Math.sin(dLon/2) * Math.sin(dLon/2)

  c = 2 * Math.atan2(Math.sqrt(cosines_product), Math.sqrt(1-cosines_product)) 
  return radius * c # Distance in kilometer
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top