Victor Krusenstråhle Victor Krusenstråhle - 2 months ago 11x
Swift Question

Swift MapKit custom current location marker

This is what my

currently looks like. My questions is, how do I change the blue ball (current location) to a custom image or icon?


I am sure you know that a user is used to seeing that blue-dot as the current user's location. You shouldn't change it unless you have a good reason.

Here is how to change it:

Set the delegate for the mapView, and then override the following function... something like this:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        let pin = mapView.viewForAnnotation(annotation) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
        pin.pinTintColor = UIColor.purpleColor()
        return pin

    } else {
        // handle other annotations

    return nil

and to have an image displayed instead: Just replace the code inside if statement with the following code:

let pin = mapView.viewForAnnotation(annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
pin.image = UIImage(named: "user_location_pin")
return pin