问题
I am working on Google Map polygon marker, I have predefined lat/long array, and need to set polygon area, It is working fine for me, but when I drag the marker polygon line doesn't change, it should have to be change as I drag the marker.
Here is my problem, I'll put a picture to illustrate it easier.
here is the code :
myMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
updateMarkerLocation(marker);
drawPolygon(coordinates);
}
private void updateMarkerLocation(Marker marker) {
LatLng latLng = (LatLng) marker.getTag();
int position = coordinates.indexOf(latLng);
if (position >= 0) {
coordinates.set(position, marker.getPosition());
marker.setTag(marker.getPosition());
}
}
@Override
public void onMarkerDragEnd(Marker marker) {
}
});
}
private void drawPolygon(List<LatLng> coordinates) {
if (polygon != null) {
polygon.remove();
}
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.fillColor(Color.GREEN);
polygonOptions.strokeColor(Color.GREEN);
polygonOptions.strokeWidth(3);
polygonOptions.addAll(coordinates);
polygon = myMap.addPolygon(polygonOptions);
}
like above image my marker move but not make polygon.
coordinates contain polygon coordinates from sqlite
回答1:
It looks like you're using the Marker tag to store the last displayed coordinate (LatLng) of the polygon point. The coordinates to be displayed are stored in coordinates yes?
If so then I'm guessing your code should be:
private void updateMarkerLocation(Marker marker) {
LatLng latLng = (LatLng) marker.getTag();
int position = coordinates.indexOf(latLng);
if (position >= 0) {
coordinates.set(position, marker.getPosition());
marker.setTag(marker.getPosition());
}
}
来源:https://stackoverflow.com/questions/61527262/google-maps-user-editable-polygon-with-fixed-number-of-points-in-android