Расстояние между двумя точками неправильное

Я использовал алгоритм на http://www.movable-type.co.uk/scripts/latlong.html, чтобы найти расстояние между двумя точками.

Мои две точки

long1 = 51.507467;
lat1 = -0.08776;

long2 = 51.508736;
lat2 = -0.08612;

Согласно скрипту Movable Type, ответ равен 0,1812 км.

Мое приложение дает результат (d) как 0,230 км.

Проверьте формулу Хаверсина: 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;

person Ally    schedule 15.07.2010    source источник


Ответы (3)


Зачем заново изобретать собственный калькулятор расстояний? Он встроен в Местоположение. класс.

Проверить

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.
person Pentium10    schedule 15.07.2010

Ваша реализация правильная. Расстояние с учетом этих долгот и широт должно давать расстояние 0.230 km. Однако обычным вводом координат является (широта, долгота). Ввод их в обратном порядке (долгота, широта) дает неправильное расстояние 0.1812 km.

person Robert Hui    schedule 15.07.2010
comment
О... немного смущен. Спасибо за вашу помощь :) - person Ally; 15.07.2010

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, ваша концепция была правильной. Может немного измениться в этой строке double c = 2 * Math.asin(Math.sqrt(a));

person Android Girl    schedule 15.05.2012