我正在开发一个 Android 谷歌地图活动项目。我应该计算 onLocationchange 的行进距离。在计算距离时,我遇到了一个问题。此处的实际要点是在触发 onlocationchange 时在文本视图中显示行进的距离。这是我尝试过的代码。
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
GeoPoint point = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude() *1E6));
path.add(point);
controller.animateTo(point);
mapview.invalidate();
distance();
}
public void distance(){
start = path.get(0);
stop = path.get(path.size()-1);
double lat1 = start.getLatitudeE6() / 1E6;
double lat2 = stop.getLatitudeE6() / 1E6;
double lon1 = start.getLongitudeE6() / 1E6;
double lon2 = stop.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.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = c * 6378.1;
d =Double.parseDouble(new DecimalFormat("####.###").format(d));
text.setText(" distance :" + d + "km");
}
我正在通过 DDMS Manual Decimal 传递四个位置这是这些位置。
1)lat -122.084095 This is the start point. and the text view is set to 0kms
long 37.422006
2)lat -122.085095 The textview distance is set to 0.088kms
long 37.422006
3)lat -122.085095 The textview distance is set to 0.142kms wroks fine till here
long 37.423006
4)lat -122.084095 The text view distance is set to 0.111kms WTF. now why is that my distance is decreased from 0.142kms to 0.111kms
long 37.423006
5)4)lat -122.084095 The text view distance is set to 0.0kms OMG.. Now the textview shows 0.0kms whats wrong.
long 37.422006
这是计算距离的正确方法吗?我在做什么错误。我认为我们应该为点数组循环。但是怎么做??我不知道...请帮助。提前致谢