1

从数组中删除 formattedCoordinate 的适当命令是什么?我知道 Objective-C 存在 removeObject,但是 Swift 呢?

var markersArray: Array<CLLocationCoordinate2D> = []

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker){
    let lat: CLLocationDegrees = marker.position.latitude
    let lng: CLLocationDegrees = marker.position.longitude
    var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
    //markersArray.remove(formattedCoordinate) need to fix this
    self.clean()
    //    return 0; not sure if we need this here
}

func mapView(_ mapView: GMSMapView, didBeginDragging marker: GMSMarker){
    let lat: CLLocationDegrees = marker.position.latitude
    let lng: CLLocationDegrees = marker.position.longitude
    var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
    // markersArray.remove(formattedCoordinate) need to fix this
}
4

1 回答 1

1

我会建议

self.markersArray = self.markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })
于 2017-04-02T20:02:40.697 回答