0

I have the following code:

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)
self.clean()
}

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)
}

As you can see I am trying to "remove" the "formattedCoordinate" from markersArray. I saw that there are options like .filter and other methods which enables removing an element at a specific index, but here I would like to remove that specific coordinate from the array. How do I do that?

konyv12
  • 716
  • 2
  • 8
  • 23

1 Answers1

0
    markers = markers.filter({ (coordinateToRemove) -> Bool in
        return coordinateToRemove.latitude == formattedCoordinate.latitude && coordinateToRemove.longitude == formattedCoordinate.longitude
    })

I had the same problem solved it by rounding off the values of latitude and longitude(as they can be different by some decimal in formattedCoordinate Object) that you are comparing using

round(coordinateToRemove.latitude)

and comparing them separately.

Agent Smith
  • 2,873
  • 17
  • 32