如何在iOS (Swift)中实现这一功能?

4

我是一名初学者程序员。

我正在开发一个谷歌地图应用,当点击一个按钮时,我希望所有点击的坐标(我已经有了这个)停止记录,并开始填充新的数组。这是为了保存地图上的坐标。

最好的方法是什么?

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
}

这个问题太宽泛了。请发布您正在进行的实现工作。 - JAL
已添加,以下是翻译的文本。 - konyv12
2个回答

1
使用for循环并将每个坐标添加到数组中。
for point in points {
   // This will loop through all the points and add them to pointsArray
   pointsArray.append(point)
}

我的问题是,我如何在实际中将数据“保存”到数组中,并在单击按钮或任何事件时开始在另一个数组中记录新的传入数据。 - konyv12
但是如果我理解正确的话,您已经拥有一些形式的数组或JSON格式的数据。 - Khalid Afridi
好的,那么我如何通过点击按钮将不同的元素添加到数组中呢? - konyv12
这是我在回答中告诉你的,使用for循环,因为我将在我的答案中给出一个for循环示例。 - Khalid Afridi
好的,但是我该如何将这些数组彼此分离? - konyv12
数组是本质上独立的,你只能为特定类型创建一个数组。但是如果你需要一个单独的数组,你可以创建新的数组。 - Khalid Afridi

1
我假设你的代码中有以下内容:

我假设你的代码中有:

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]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接