在Android中行走/驾驶时,在Google Maps V2上更新位置标记并绘制路径
我想在开车或走路时在Google Maps V2上绘制路径,但我找不到任何在线资源来帮助我解决这个问题。 我正在使用onLocationChanged侦听器方法。在Android中行走/驾驶时,在Google Maps V2上更新位置标记并绘制路径,android,google-maps,Android,Google Maps,我想在开车或走路时在Google Maps V2上绘制路径,但我找不到任何在线资源来帮助我解决这个问题。 我正在使用onLocationChanged侦听器方法。 谢谢。您可以使用谷歌地图中的多段线。它获取位置并在地图上绘制它们。如前所述,您需要使用多段线对象,您可以收集所有路径点(LatLng位置),并使用环路在地图上添加所有道路: Polyline newPolyline; GoogleMap mMap = ((SupportMapFragment)getSupportFragmentMan
谢谢。您可以使用谷歌地图中的多段线。它获取位置并在地图上绘制它们。如前所述,您需要使用
多段线
对象,您可以收集所有路径点(LatLng
位置),并使用环路在地图上添加所有道路:
Polyline newPolyline;
GoogleMap mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
for(int i = 0 ; i < directionPoints.size() ; i++)
{
rectLine.add(directionPoints.get(i));
}
newPolyline = mMap.addPolyline(rectLine);
要更新标记的位置,只需删除旧标记并创建一个新标记(使用新坐标)。扩展OnMyLocationChangeListener 在地图设置中使用此选项:
mMap.setOnMyLocationChangeListener(this);
覆盖以下内容:
@Override
public void onMyLocationChange(Location location) {
/* your code here */
}
例如,代码可以是这样的(未测试):
locationToLatLng可能看起来像这样:
public static LatLng locationToLatLng(Location loc) {
if(loc != null)
return new LatLng(loc.getLatitude(), loc.getLongitude());
return null;
}
嗨,Emil Adz,谢谢你的回答,但是我需要在走路/开车的时候划一条线,当我移动的时候,这条线应该是活的。。。。我在使用onLocationChanged方法。有没有任何方法可以匹配我的场景。正如我在第二部分中所说:“在每个位置更新上添加一行”,所以将第二个代码段放入onLocationChanged中method@Emil我有一个类似的问题,除了我的是动画标记从实时流的位置对象,而不是绘制多段线。
Location lastLocationloc=null;
@Override
public void onMyLocationChange(Location location) {
if(lastLocationloc == null)
lastLocationloc = location;
LatLng lastLatLng= locationToLatLng(lastLocationloc);
LatLng thisLatLng= locationToLatLng(location);
mMap.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(3).color(Color.RED));
lastLocationloc = location;
}
public static LatLng locationToLatLng(Location loc) {
if(loc != null)
return new LatLng(loc.getLatitude(), loc.getLongitude());
return null;
}