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

时间:2017-04-06 20:33:07

标签: ios swift google-maps

我是初学程序员。

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

最好的方法是什么?

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
}

2 个答案:

答案 0 :(得分:1)

使用for循环并在数组中添加每个坐标

for point in points {
   // This will loop through all the points and add them to pointsArray
   pointsArray.append(point)
}

答案 1 :(得分: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()

您现在可以将积分添加到新的&#34;设置&#34; ...将该组添加到&#34;数组数组&#34; ......然后以这种方式回头告诉他们:

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