1

What would be an appropriate command for removing formattedCoordinate from the array? I know a removeObject exists for Objective-C, but how about 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
}
Hamish
  • 78,605
  • 19
  • 187
  • 280
konyv12
  • 716
  • 2
  • 8
  • 23
  • Stick an at: in front of formattedCoordinate in your line with remove – Tad Donaghe Apr 02 '17 at 20:02
  • See the swift language guide section on collections: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105 – Tad Donaghe Apr 02 '17 at 20:02
  • @Konyv12 array.remove(at: Index).provide the index value – Tushar Sharma Apr 02 '17 at 20:04
  • With the dupe target in mind, the only remaining problem is that `CLLocationCoordinate2D` doesn't (for whatever reason) conform to `Equatable` – doing so is fairly trivial, see for example http://stackoverflow.com/a/27364437/2976878 – Hamish Apr 02 '17 at 20:14
  • But the user might click on marker that was drawn previously, so it's not always the last member of the array that needs to be removed. – konyv12 Apr 02 '17 at 20:22

1 Answers1

1

I would suggest

self.markersArray = self.markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })
JuicyFruit
  • 2,638
  • 2
  • 18
  • 35