簡體   English   中英

谷歌地圖Android api v2折線長度

[英]Google Maps Android api v2 polyline length

我正在嘗試找到android maps api v2方法,它將決定我在移動時創建的折線的長度。 我會把它放在onLocationChanged()中以進行持續更新。 任何人都知道方法是什么以及地圖api將顯示長度的單位是多少?

Polyline line = map.addPolyline(new PolylineOptions());


public void onLocationChanged(Location location) {



line.add(new LatLng(location.getLatitude(), location.getLongitude())
 .width(5)
 .color(Color.RED));

}

我遇到過同樣的問題。 這是我能夠解決它的方式。

  1. private ArrayList<LatLng> mLatLngList; 在位置更改時將位置添加到ArrayList。
  2. 使用Gradle將此http://googlemaps.github.io/android-maps-utils/庫導入您的項目或compile 'com.google.maps.android:android-maps-utils:0.4'
  3. 導入庫import com.google.maps.android.SphericalUtil;
  4. double distance = SphericalUtil.computeLength(mLatLngList);

您可以將最后一個位置的Location.distanceBetween用於當前位置。 如果您想要距離起點和終點位置的總距離,請隨着位置的變化保持總計

我有同樣的問題。 這是我的解決方案,其中points是PolylineOptions對象。

protected float calculateMiles() {
    float totalDistance = 0;

    for(int i = 1; i < points.getPoints().size(); i++) {
        Location currLocation = new Location("this");
        currLocation.setLatitude(points.getPoints().get(i).latitude);
        currLocation.setLongitude(points.getPoints().get(i).longitude);

        Location lastLocation = new Location("this");
        currLocation.setLatitude(points.getPoints().get(i-1).latitude);
        currLocation.setLongitude(points.getPoints().get(i-1).longitude);

        totalDistance += lastLocation.distanceTo(currLocation);


    }

    return totalDistance;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM