flippyflo
flippyflo

Reputation: 31

How do I use the default blue annotation for user location rather than my custom annotation?

I have custom annotations on my map and now I want to be able to show the user location as well as the custom annotations. However, when I show the user location it uses the custom annotation rather than the default blue annotation. Is there a way to make the user location use the default?

I get the users location to show using the below:

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
map.showsUserLocation = true

And the custom annotation is set up using the following:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.canShowCallout = false
    } else {
        annotationView?.annotation = annotation
    }

    annotationView?.image = UIImage(named: "Marker")

    return annotationView
}

Thanks

Upvotes: 1

Views: 487

Answers (3)

Hidayet Ozsoy
Hidayet Ozsoy

Reputation: 117

you can do it in your MKMapViewDelegate like this:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }
    
    // return custom views for other annotations...
}

Upvotes: 2

I dealing with the same problem. Use this check at the top of your "viewFor annotation" delegate method:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation as? MKUserLocation != mapView.userLocation else { return }

This will prevent from customize your User location

Upvotes: 0

DrainOpener
DrainOpener

Reputation: 196

Do it like below :

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    yourCustomAnnotation = CustomPointAnnotation()
    yourCustomAnnotation.pinCustomImageName = "Marker"
    yourCustomAnnotation.coordinate = location

    yourCustomAnnotationView = MKPinAnnotationView(annotation: yourCustomAnnotation, reuseIdentifier: "pin")
    map.addAnnotation(yourCustomAnnotationView.annotation!)
}

Upvotes: 2

Related Questions