2009-02-10 15 views
13

Tôi đang gặp sự cố tính khoảng cách bằng phương pháp Location.distanceTo.Android: Location.distanceĐể không hoạt động chính xác?

private class MyLocationOverlay1 extends MyLocationOverlay { 
    @Override 
    public void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) 
     super.drawMyLocation(canvas,mapView,lastFix,myLocation,when); 

     Location bLocation = new Location("reverseGeocoded"); 
     bLocation.setLatitude(FindList.gpslat);   // Value = 3.294391E7 
     bLocation.setLongitude(FindList.gpslong);   // Value = -9.6564615E7 
     Location aLocation = new Location("reverseGeocoded"); 
     aLocation.setLatitude(myLocation.getLatitudeE6()); // Value = 3.2946164E7 
     aLocation.setLongitude(myLocation.getLongitudeE6()); // Value = -9.6505141E7 
     aLocation.set(aLocation); // Don't think I need this 
     bLocation.set(bLocation); // Don't think I need this either 

     int distance = (int)aLocation.distanceTo(bLocation); // Value = 12637795 ??? 
     String str = " (" + String.valueOf(distance) + " meters)"; 
    } 
} 

Ai đó có thể cho biết lý do tôi tính toán khoảng cách 12.637.795 Mét của tôi?

Trả lời

14

Bạn nên Modify dòng:

aLocation.setLatitude(myLocation.getLatitudeE6()/1e6); 
aLocation.setLongitude(myLocation.getLongitudeE6()/1e6); 

Và loại bỏ các dòng:

aLocation.set(aLocation); 
bLocation.set(bLocation); 
+0

Cảm ơn bạn, Hình như tôi đã có một số vấn đề về một số thứ khác (đảo ngược mã hóa địa lý) đã được vì điều này. – Chrispix