2017-08-09 134 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

完美的作品,非常感謝 –

相關問題