Swift Google Maps does not delete polygons

醉酒当歌 提交于 2019-12-12 05:29:23

问题


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

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