在onMarkerDrag上拖动折线Android谷歌地图v2 [英] Drag Polyline onMarkerDrag Android Google Maps v2
问题描述
我目前正在开发一个Android应用程序,该应用程序将允许用户在地图上绘制带有标记的折线.现在,我想实现一个功能,通过该功能,只要拖动标记,折线就可以拖动,并在调用onMarkerDragEnd()方法时更新折线.有谁知道我怎么能做到这一点?以下是我的代码片段.谢谢!
I'm currently developing an android application that would allow users to draw Polylines with Markers on the map. Right now, I would like to implement a feature whereby the polyline will be draggable whenever the marker is being dragged and update the Polyline when the onMarkerDragEnd() method is being called. Does anyone know how I can achieve this? Below is a snippet of my codes. Thanks!
googleMap.setOnMapClickListener(new OnMapClickListener(){
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
if(drawMode == true && arrayPoints.isEmpty()){
MarkerOptions marker=new MarkerOptions();
marker.position(point);
googleMap.addMarker(marker).setDraggable(true);
arrayPoints.add(point);
marker.draggable(true);
}
else if(drawMode == true){
Log.e("","IN SECOND");
MarkerOptions marker=new MarkerOptions();
marker.position(point);
googleMap.addMarker(marker).setDraggable(true);
arrayPoints.add(point);
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.BLUE);
polylineOptions.width(5);
polylineOptions.addAll(arrayPoints);
Polyline drawRoute = googleMap.addPolyline(polylineOptions);
}
}
});
推荐答案
首先,将Polyline drawRoute设置为字段而不是局部变量. 然后,您可以通过调用drawRoute.setPoints(arrayPoints)更新onMarkerDragEnd内部的折线.
At first make Polyline drawRoute a field instead of a local variable. Then you can update the polyline inside onMarkerDragEnd by calling drawRoute.setPoints(arrayPoints).
然后,您还需要一个Java Map,以跟踪哪个标记负责数组中的哪个点.该地图将标记ID作为键,将数组索引作为值. (您可以从map.addMarker返回的标记中获取标记ID) 拖动标记时,您可以使用标记ID和上述Java Map在arrayPoints中找到相应Point的索引.这样,您可以交换数组中的点,然后再次调用drawRoute.setPoints.
Then you need in addition a Java-Map, which keeps track of which marker was responsible for which point in the array. The map would have the marker-ID as key and the array-index as value. (You get the marker ID from the marker which is returned by map.addMarker) When a marker is dragged, you can find out the index of the corresponding Point in arrayPoints using the marker-ID and said Java-Map. With that, you can exchange the point in the array and call drawRoute.setPoints again.
这篇关于在onMarkerDrag上拖动折线Android谷歌地图v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!