2010-07-15 10 views
5

Ich habe den Algorithmus auf http://www.movable-type.co.uk/scripts/latlong.html verwendet, um den Abstand zwischen zwei Punkten zu finden.Entfernung zwischen zwei Standorten ist nicht richtig

Meine zwei Punkte sind

long1 = 51.507467; 
lat1 = -0.08776; 

long2 = 51.508736; 
lat2 = -0.08612; 

Nach Movable Type Script die Antwort 0.1812km

Meine Anwendung ist das Ergebnis (d) als 0.230km

prüfen Haversine Formel ergibt: http://www.movable-type.co.uk/scripts/latlong.html

double R = 6371; // earth’s radius (mean radius = 6,371km) 
    double dLat = Math.toRadians(lat2-lat1); 

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

Antwort

9

Warum, um Ihren eigenen Entfernungsrechner neu zu erfinden, gibt es einen eingebauten Location Klasse.

Check out

distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) 
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. 
+0

Werde es ausprobieren :) – Ally

+0

Es hat funktioniert! Vielen Dank – Ally

3

Ihre Implementierung ist korrekt. Die Entfernung unter Berücksichtigung dieser Längen und Breiten sollte eine Entfernung von 0.230 km ergeben. Die normale Eingabe für Koordinaten ist jedoch (Breite, Länge). Wenn Sie sie rückwärts (Länge, Breite) einfügen, wird der falsche Abstand 0.1812 km angezeigt.

+1

Oh ... wenig verlegen ein wenig chang sein. Danke für deine Hilfe :) – Ally

1
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) { 
    double lat1 = StartP.getLatitudeE6()/1E6; 
    double lat2 = EndP.getLatitudeE6()/1E6; 
    double lon1 = StartP.getLongitudeE6()/1E6; 
    double lon2 = EndP.getLongitudeE6()/1E6; 
    double dLat = Math.toRadians(lat2-lat1); 
    double dLon = Math.toRadians(lon2-lon1); 
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2); 
    double c = 2 * Math.asin(Math.sqrt(a)); 
    return Radius * c; 
} 

Ally Ihr Konzept right.May war in dieser Linie double c = 2 * Math.asin(Math.sqrt(a));