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);
}
只要把類似的代碼在'onLocationChanged()'重寫,以更新它,當用戶的位置變化。 –
@Daniel我已經這樣做了,位置正在移動,並且工作正常。當我點擊地圖時,多段線被繪製並且藍點在它上面移動。我想一直更新折線。你知道任何解決方案 –
顯示onLocationChanged代碼 –