簡體   English   中英

我如何在iOS(swift)中實現這一目標?

[英]How do I achieve this in 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
}

使用for循環並在數組中添加每個坐標

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

我假設你的代碼中有某個地方:

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM