Google Maps user-editable polygon with fixed number of points in Android?

梦想与她 提交于 2020-05-07 09:24:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!