1

私は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ループが必要だと思います。しかし、どうやって?? わかりません...助けてください。よろしくお願いします。

4

1 に答える 1

1

元のポイントに戻ったときに距離が 0 であると言っているという事実は、電球を点灯させる必要があります。実際に計算しているのは、合計距離ではなく、元のポイントからエミュレートしている新しいポイントまでの距離です。パスに沿って。

ポイント 1 から他のすべてのポイントまでの距離を測定する代わりに、最後にいたポイントから次のポイントまでの距離を測定し、それを合計距離を保持するために作成した距離変数に追加する必要があります。

たとえば、次のように変更すると問題が解決するはずです。

start = path.get(path.size()-2);
//Be sure to check that there's at least 2 points in your array with an if statement!!

じゃあ後で:

totalDistance += d;
textView.setText("Text" + totalDistance);

ここで、totalDistance は前に定義した double です。

于 2013-02-23T00:04:33.060 に答える