JamesVoo
JamesVoo

Reputation: 171

How to change markerTintColor in MapKit without changing default location pin in Swift?

I have MKMarkerAnnotationView to change color of pins on my map.

func mapView(_ MapView:MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{

    let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")

    view.markerTintColor = .blue

    return view

}

But, when I start my app, marker of my defoult location changes to. How can I change pin without changing this marker? Code to view location is also simple

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    self.MapView.showsUserLocation = true
}

Thanks for answer! :)

Upvotes: 0

Views: 1146

Answers (2)

LorenzOliveto
LorenzOliveto

Reputation: 7936

You could check if the annotation is the user location like this:

func mapView(_ MapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
    view.markerTintColor = .blue
    return view
}

Upvotes: 5

Duncan C
Duncan C

Reputation: 131398

In your method, check to see if the annotation object is an instance of MKUserLocation. If it is, return nil to keep the standard user location annotation view.

(The Docs for mapView(_:viewFor:) explain this.)

Upvotes: 0

Related Questions