2016-03-15 135 views
0

1)我找到一种方法来显示地图上的折线实时位置更新用户。我已经这样做,但它不准确。下面是显示我的GIF我正在努力实现。谷歌地图的实时路线和显示路线的历史

实施例:

enter image description here

2)亦一旦用户用它做,我需要保存的路线,并通过与折线地图上绘制再次显示它。

以下是我所做的。

在点击按钮,我触发与MIN_TIME_BW_UPDATES = 10sec0 meters

@Override 
    public void onLocationChanged(Location location) { 

     LatLng current = new LatLng(location.getLatitude(), location.getLongitude()); 
     if (flag == 0) //when the first update comes, we have no previous points,hence this 
     { 
      prev = current; 
      flag = 1; 
     } 
     CameraUpdate update = CameraUpdateFactory.newLatLngZoom(current, 18); 
     googleMap.animateCamera(update); 
     googleDirection = new GoogleDirection(new TaskCompleted() { 
      @Override 
      public void callBack(List<List<HashMap<String, String>>> routes) { 
       addPolyLineToMap(routes); 
      } 
     }); 
     googleDirection.execute(prev, current, "mode=walking"); 
     prev = current; 
    } 

googleDirection的位置更新,只是用纬度,经度和行驶模式下API调用。 addPolyLineToMap是一种将多段线添加到地图的方法。

请有利于优化我的逻辑

回答

0

你说GoogleDirection只是一个API和addPolyLineToMap是方法。所以我明白,在onLocationChanged方法中插入该代码是不恰当的,该方法在用户位置更改时触发。您可以将其功能是通过方法优化你的代码,

public void onLocationChanged(Location location){ 
    current = location; 
    //Update Map or UpdateUI 
    updateCamera(); 
    updatePolyline(); 
} 

private void updateCamera(){ // } 
private void updatePolyline(){ // } 

你可以找到在这个环节的完整的源代码,虽然它的Pubnub源然而,将是有益的。 链接:https://www.pubnub.com/blog/2015-05-12-broadcasting-realtime-location-data-for-android-live-updating-map/