简体   繁体   中英

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

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

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());
  }
}

I think you're using the tools you have a little inefficiently. My recommendation would be to store an array of markers, and use markerTag to store the index of your point, so in drawPolygon

for (int i = 0; i < coordinates.size(); i++) {
   markers[i].setTag(i); //Associate a marker with a point
}

Then, when you move the marker, you would replace the co-ordinate at that point like so:

points[marker.getTag().toInt()] = marker.getTag().getPosition();

Sorry this is so vague. I use kotlin, so I've probably gotten some of the syntax wrong, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM