delete al observers and kept one?

is it possible to delete all observers and kept one( on user current location)

because my app keep cloning my custom annotation non-stop

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
       
        map.mapType = MKMapType.standard
        
        let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
        let region = MKCoordinateRegion(center: locValue, span: span)
        map.setRegion(region, animated: true)
        
        
         let annotation = MKPointAnnotation()
        
         annotation.coordinate = locValue
         annotation.title = "Yo"
        
         annotation.subtitle = "current location"
         map.addAnnotation(annotation)
       
        
        
        self.latitud.text = String(manager.location!.coordinate.latitude)
        self.longitud.text = String(manager.location!.coordinate.longitude)
        UserDefaults.standard.set(latitud.text, forKey: "latitudeConductor")
        UserDefaults.standard.set(longitud.text, forKey: "longitudConductor")
        
        
    }
    
// func for custom annotation
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation{
            return nil
        }
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
        annotationView.image = UIImage(named: "Puntero_carrito.2-2")
        annotationView.canShowCallout = true
        
        return annotationView
    }
    
  

Replies

This is not a question about a feature of the Swift language, so it’s posted in the wrong forum. Maps and Location might be better.


didUpdateLocations is called many times. So of course when you’re adding an annotation each time, you’re going to end up with a lot of them. You should create and add your annotation only once. You could do that in viewDidLoad, but that might be misleading since you don’t have a valid location yet. I’d do it where you have it in didUpdateLocations, but only the first time. Specifically, make an instance var instead of a local variable (declare your “var annotation” at the class level and make it optional). Only create and add it if it was nil.


Then with each update, just assign the new location to the old annotation. MapKit will move it for you. Don’t create or add any new annotation every time.


Speaking of locations. Do not use manager.location!. Use the “locations” parameter passed in to that method. You can just use the last element of the array if you don’t care about skipping any possible intermediate locations and just want to move to the latest. It is very dangerous to force unwrap a property that is specifically documented to return nil under some conditions. Granted, in didUpdateLocations it will probably not be nil, but it’s a very bad habit to force unwrap an optional like that without checking for nil.