2017-08-09 99 views
3

当我点击地图上的某个地方时,应用程序在当前位置和目的地之间绘制折线(点击)。我希望那个目的地标记一直点击自己(每隔几秒),所以当用户移动新的多段线正在绘制时(更新最后一个)。Android谷歌地图标记不断点击相同的点

我试着用处理程序和一堆东西,并没有找到解决方案。

任何答案将不胜感激。

private ArrayList<LatLng> mClickedPoints; 

/** Setting onclick listener for the map */ 
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 

    @Override 
    public void onMapClick(LatLng point) { 

     /** 
     * Clearing map if user clicks on map more then one time. 
     * Reseting View widgets and arrays, adding points of interests 
     */ 
     if (mClickedPoints.size() > 1) { 
      mMap.clear(); 
      mClickedPoints.clear(); 
      mClickedPoints = new ArrayList<>(); 
     } 

     /** Adding current location to the ArrayList */ 
     mClickedPoints.add(start); 
     Log.i("current location", start.toString()); 

     MarkerOptions options = new MarkerOptions(); 
     options.position(start); 

     /** Destination click */ 
     mClickedPoints.add(point); 

     /** Setting the position of the marker */ 
     options.position(point); 

     /** Checks if start and end locations are captured */ 
     if (mClickedPoints.size() >= 2) { 
      orig = mClickedPoints.get(0); 
      dest = mClickedPoints.get(1); 
     } 
     mMap.addMarker(options); 
     request_directions_and_get_response(); 
    } 
}); 

onLocationChanged()重写:

@Override 
public void onLocationChanged(Location location) { 

    mLastLocation = location; 
    if (mCurrLocationMarker != null) { 
     mCurrLocationMarker.remove(); 
    } 

    /** Setting current location marker */ 
    start = new LatLng(location.getLatitude(),location.getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(start); 
    markerOptions.title("Current Location"); 
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
    mCurrLocationMarker = mMap.addMarker(markerOptions); 
} 
+0

只要把类似的代码在'onLocationChanged()'重写,以更新它,当用户的位置变化。 –

+0

@Daniel我已经这样做了,位置正在移动,并且工作正常。当我点击地图时,多段线被绘制并且蓝点在它上面移动。我想一直更新折线。你知道任何解决方案 –

+0

显示onLocationChanged代码 –

回答

1

为了使用点击的点ArrayList中,你有现在,你需要删除以前的原点,然后添加当前原点到ArrayList的开始,然后根据新的当前位置获取重新计算的方向。

事情是这样的:

@Override 
public void onLocationChanged(Location location) { 

    mLastLocation = location; 
    if (mCurrLocationMarker != null) { 
     mCurrLocationMarker.remove(); 
    } 

    /** Setting current location marker */ 
    start = new LatLng(location.getLatitude(),location.getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(start); 
    markerOptions.title("Current Location"); 
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
    mCurrLocationMarker = mMap.addMarker(markerOptions); 

    //If there is a previous route drawn: 
    if (mClickedPoints.size() >= 2) { 
     //remove the old origin: 
     mClickedPoints.remove(0); 

     //add the new one at the 0th position: 
     mClickedPoints.add(0, start); 

     //set orig and dest for directions: 
     orig = mClickedPoints.get(0); 
     dest = mClickedPoints.get(1); 

     //get updated directions: 
     request_directions_and_get_response(); 
    } 

} 
+0

完美的作品,非常感谢 –

相关问题