Tomas Lo Pinto
Tomas Lo Pinto

Reputation: 43

Custom Image on MKPointAnnotation

I'd like to replace the default MKPointAnnotation logo with a custom one.

So I wrote this code in my MapViewController:

import UIKit
import MapKit
import CoreLocation



class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate  {

@IBOutlet weak var myMap: MKMapView!
let locationManager = CLLocationManager()
var monPin:CustomPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!


override func viewDidLoad() {
    super.viewDidLoad()

    //Mark: - Authorization
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    myMap.delegate = self
    myMap.mapType = MKMapType.standard
    myMap.showsUserLocation = true


    let location = CLLocationCoordinate2D(latitude: 43.2885108, longitude:5.3855545000000124)
    let center = location
    let region = MKCoordinateRegionMake(center, MKCoordinateSpan(latitudeDelta: 0.025, longitudeDelta: 0.025))
    myMap.setRegion(region, animated: true)

    monPin = CustomPointAnnotation()
    monPin.pinCustomImageName = "mapIcon"
    monPin.coordinate = location
    monPin.title = "Mon titre"
    monPin.subtitle = "mon Sous titre"


    pinAnnotationView = MKPinAnnotationView(annotation: monPin, reuseIdentifier: "pin")
    myMap.addAnnotation(pinAnnotationView.annotation!)



}


//MARK: - Custom Annotation
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {


    let reuseIdentifier = "pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier:reuseIdentifier)

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

    let customPointAnnotation = annotation as! CustomPointAnnotation
    annotationView?.image = UIImage(named: customPointAnnotation.pinCustomImageName)

    return annotationView
}

Where monPin.pinCustomImageName = "mapIcon" refer to my xassets

But it only display a classic MKPointAnnotation picture (this one: https://i.sstatic.net/pD82c.png)

**And I think the problem is that: ** the function mapView is not called anywhere because I tried a simple print("hello") in it and it does not appear in the console and even if I erase all this function code, it does not change anything to my app.

That's why I'm wondering how I could face this problem.

I don't understand 100% of my code because I took it from a kind stack overflow user, I mean I understand all the code except the mapView() part.

Upvotes: 0

Views: 737

Answers (1)

OOPer
OOPer

Reputation: 47876

I believe Xcode has shown a warning if you have created a new project with default settings. (You should not ignore any of the warnings.)

Instance method 'mapView(mapView:viewForAnnotation:)' nearly matches optional requirement 'mapView(_:viewFor:)' of protocol 'MKMapViewDelegate'

This line of your code:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

does not implement any of the protocol methods declared in MKMapViewDelegate in Swift 3.

And the quick fix feature (choose the first one) fixes it as:

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

If your Xcode does not help you fix if, you may need to make it by yourself.

Anyway, with the fix shown above, the method should be called.

Upvotes: 1

Related Questions