问题
I'm developing an app with Swift 3.0 and working with the Google Maps SDK. I arrived at the point that by clicking on the map I draw a polygon where at each vertex I add a marker. Like the image given below:
What I want to do is when I move a marker. With the "didDrag" event, repaint the new polygon with the new markers' positions. But this happens:
The code is:
//Path Line and Polygon
var pathLine = GMSMutablePath()
//Create Line of Poligon
var linePolygon = GMSPolyline()
//All markers and marker
var allMarker = [GMSMarker]()
var marker = GMSMarker()
//Creat Polygon with "pathLine"
var polygon = GMSPolygon()
//Save first point and marker of polygon
var startPolygon = CLLocationCoordinate2D()
var startmarker = GMSMarker()
//When tap in a marker
func mapView(_ mapView: GMSMapView, didDrag marker: GMSMarker) {
print(marker.position.longitude)
print(marker.position.latitude)
mapView.clear()
for i in 0...allMarker.count - 1{
let marker = GMSMarker(position: allMarker[i].position)
marker.map = mapView
pathLine.add(allMarker[i].position)
polygon = GMSPolygon(path: pathLine)
polygon.map = mapView
linePolygon = GMSPolyline(path: pathLine)
linePolygon.strokeColor = .orange
linePolygon.strokeWidth = 3
linePolygon.map = mapView
}
}
The problem is that instead of leaving the last polygon, it shows all the polygons and they are not deleted with mapView.clear (). How can I do it?Thanks :)
来源:https://stackoverflow.com/questions/45101209/swift-google-maps-does-not-delete-polygons