我如何在iOS(swift)中实现这一目标?

我是初学程序员.

我正在开发一个谷歌地图应用程序,只需点击一下按钮,我希望所有的点击坐标(我已经有了这个)停止录制,而是开始填充新的数组.这是为了保存地图的坐标.

最好的方法是什么?

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
    var lat: CLLocationDegrees = coordinate.latitude
    var lng: CLLocationDegrees = coordinate.longitude
    var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
    markersArray.append(formattedCoordinate)
    mapView.clear()
    addMarker()
    drawPolygon()
}

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    var lat: CLLocationDegrees = marker.position.latitude
    var lng: CLLocationDegrees = marker.position.longitude
    var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
    markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })
    mapView.clear()
    drawPolygon()
    addMarker()
    return true
}

func mapView(_ mapView: GMSMapView, didBeginDragging marker: GMSMarker){
    var lat: CLLocationDegrees = marker.position.latitude
    var lng: CLLocationDegrees = marker.position.longitude
    var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
    markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude))})
}

func mapView(_ mapView: GMSMapView, didDrag marker: GMSMarker){
    var lat: CLLocationDegrees = marker.position.latitude
    var lng: CLLocationDegrees = marker.position.longitude
    var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
    markersArray.append(formattedCoordinate)
    mapView.clear()
    drawPolygon()
    addMarker()
    markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude))})
    }

func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker){
    var lat: CLLocationDegrees = marker.position.latitude
    var lng: CLLocationDegrees = marker.position.longitude
    var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
    markersArray.append(formattedCoordinate)
    mapView.clear()
    drawPolygon()
    addMarker()
    }

func addMarker(){
    for coordinate in markersArray {
        let marker = GMSMarker()
        marker.position = coordinate
        marker.map = mapView
        marker.isDraggable = true
        marker.icon = GMSMarker.markerImage(with: UIColor.blue)
    }
}

func drawPolygon(){
    let path = GMSMutablePath()
    var i = 0
    // get path for polygon
    for coordinate in markersArray {
        path.add(coordinate)
        i = i+1
    }
    // build polygon
    let polygon = GMSPolygon(path:path)
    polygon.map = nil;//not sure if this line is redundant
    polygon.fillColor = UIColor.green
    polygon.strokeColor = UIColor.black
    polygon.strokeWidth = 1
    polygon.map = mapView
}

最佳答案 我假设你的代码中有某个地方:

var markersArray = [CLLocationCoordinate2D]()

它定义了你的坐标数组.

您的代码是向该数组添加点,如下所示:

markersArray.append(formattedCoordinate)

如果要保存坐标的“集合”,请在定义markersArray的位置添加:

var arrayOfMarkersArrays = Array<Array<CLLocationCoordinate2D>>()

然后,在按钮上点按,您可以这样做:

// add the current array of points to the "array of arrays"
arrayOfMarkersArrays.append(markersArray)

// "reset" your tracking array
markersArray.removeAll()

您现在可以将点添加到新的“集合”…将该集添加到“数组数组”…然后以这种方式返回它们:

// get the first "set" of coordinates
markersArray = arrayOfMarkersArrays[0]
点赞