2014-11-21 27 views
0

我監視當前位置並註冊GoogleMap.MyLocationChangeListener。我想畫一條代表我自己路線的折線(地圖上的曲目)。在每個位置更新中,我想爲路線添加新的點,以便更新地圖上的軌道。如何將位置點動態添加到Android GoogleMap中的Polyline中?

這裏是我的代碼無法正常工作:

private GoogleMap mMap; 
private boolean drawTrack = true; 
private Polyline route = null; 
private PolylineOptions routeOpts = null; 

private void startTracking() { 
    if (mMap != null) { 
     routeOpts = new PolylineOptions() 
       .color(Color.BLUE) 
       .width(2 /* TODO: respect density! */) 
       .geodesic(true); 
     route = mMap.addPolyline(routeOpts); 
     route.setVisible(drawTrack); 

     mMap.setOnMyLocationChangeListener(this); 
    } 
} 

private void stopTracking() { 
    if (mMap != null) 
     mMap.setOnMyLocationChangeListener(null); 

    if (route != null) 
     route.remove(); 
     route = null; 
    } 
    routeOpts = null; 
} 

public void onMyLocationChange(Location location) { 
    if (routeOpts != null) { 
     LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     routeOpts.add(myLatLng); 
    } 
} 

如何添加點的折線,這樣的變化將在UI中反映出來?現在,折線不被渲染。

我使用最新的play-services:6.1.71(截至此日期)。

回答

1

這似乎爲我工作:

public void onMyLocationChange(Location location) { 
    if (routeOpts != null) { 
     LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     List<LatLng> points = route.getPoints(); 
     points.add(myLatLng); 
     route.setPoints(points); 
    } 
} 

有沒有更好的辦法?

相關問題