私はAndroidのGoogleマップアクティビティプロジェクトに取り組んでいます。Locationchangeで移動距離を計算する必要があります。ここで私は距離を計算するときに問題に直面しています。ここでの実際のポイントは、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");
}
そして、私はDDMSManualDecimalを介して4つの場所を通過しています。これらの場所は次のとおりです。
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
これは距離を計算する正しい方法ですか。私がしている間違いは何ですか。ポイントの配列のforループが必要だと思います。しかし、どうやって?? わかりません...助けてください。よろしくお願いします。