2010-07-15 5 views
5

http://www.movable-type.co.uk/scripts/latlong.html에있는 알고리즘을 사용하여 두 점 사이의 거리를 확인했습니다.두 위치 간 거리가 올바르지 않음

내 두 지점 대답은 내 응용 프로그램은 0.230km 같은 결과 (d)를 제공 0.1812km

입니다 Movable Type Script에 따르면

long1 = 51.507467; 
lat1 = -0.08776; 

long2 = 51.508736; 
lat2 = -0.08612; 

입니다

확인 하버 사인 공식 : 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; 

답변

9

왜 자신의 거리 계산기를 재창조해야하는지, Location 클래스에 내장 된 계산기가 있습니다.

체크 아웃

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

그것을 시도해보십시오 :) – Ally

+0

그것은 일했습니다! 감사 – Ally

3

귀하의 구현이 정확합니다. 그 경도와 위도가 주어진 거리는 0.230 km의 거리를 만들어야합니다. 그러나 좌표의 일반 입력은 (위도, 경도)입니다. 거꾸로 (경도, 위도) 넣으면 0.1812 km의 잘못된 거리가 생성됩니다.

+1

아 ... 조금 당황에 약간 장을합니다. 도와 주셔서 감사합니다 :) – 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; 
} 

앨리 당신의 개념이었다 안색이 줄 double c = 2 * Math.asin(Math.sqrt(a));

관련 문제