2014-01-17 72 views
3

我试图找到android maps api v2方法,它将确定我在移动时创建的折线的长度。我会把它放在onLocationChanged()中以便不断更新。任何人都知道这个方法是什么,地图api会显示什么单位的长度?谷歌地图安卓api v2折线长度

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


public void onLocationChanged(Location location) { 



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

} 
+0

吗? –

+0

@mohammedmomn我只需要在移动时创建的多段线的长度。是的,我想让它画出路径,但更重要的是我需要路径长度。谢谢 – johnsonjp34

+0

也许[this](http://stackoverflow.com/questions/19479667/how-can-i-calculate-a-current-distance-to-polyline-points-on-google-maps-v2-in-a )可以帮助你 – Aiapaec

回答

4

您可以在上一个位置使用Location.distanceBetween到当前位置。如果你想从你的开始和结束位置的总距离,然后保持你的位置变化的运行总和

+0

这是我不想做的。想象一下,我的车在没有网络区域的一段时间。整个计算会出错。没有一个直接的api,它需要一个点的列表,然后给出一个距离 –

+0

为什么它有关系,如果你有一个互联网连接或不distance'etween'没有网络电话 – tyczj

2

我有同样的问题。这里是我的解决方案,其中点是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; 
} 
+0

和'.distanceTo'定义? – kosemuckel

+0

它在位置对象https://developer.android.com/reference/android/location/Location.html#distanceTo (android.location.Location)中定义, – MikeSchem

3

我有同样的问题。这是我能够解决它的方式。

  1. private ArrayList<LatLng> mLatLngList; 将位置添加到位置更改上的ArrayList。
  2. 使用摇篮导入此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);