giorgio
giorgio

Reputation: 105

MKMapview delegate never called even with set delegate

I have a UIView that integrate a scrollview. Inside the scrollview I have a map where a user need to move a pin. I'm gonna reuse the location of pin later.

I'm setting strong reference and setting delegate ( both design and code) for the map. I'm able to see the pin created but to have custom icon and possibility to set it draggable, I have implement the mapView( viewFor MKAnnotation

This seems to be never called and I'm not getting why.

I also try to get another delegate as didfinishloading to understand if problem is related to the mkannotation but also this one is never been called.

import Foundation
import Firebase
import MapKit
import UIKit


public class Setup: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate{


    var ChargePointID = ""
    @IBOutlet weak var chargepointIDLabel: UILabel!


    let locationManager = CLLocationManager()
    var pin: customPin!
    //setting up variables
    //pricing textfield and slider
    @IBOutlet weak var priceLabel: UITextField!
    @IBOutlet weak var priceSlider: UISlider!


    @IBOutlet var map: MKMapView!

    override public func viewDidLoad() {
        super.viewDidLoad()
         chargepointIDLabel.text = ChargePointID
        self.map.delegate = self
        self.map.mapType = .hybrid
        // Ask for Authorisation from the User.
        self.locationManager.requestAlwaysAuthorization()

        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }

        //create liocation relative to user
        let location = CLLocationCoordinate2D(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

        //centering map to user location
        let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
        self.map.setRegion(region, animated: true)

        //creating a pin of type custompin
        pin = customPin(pinTitle: ChargePointID, pinSubTitle: "", location: location)

        map.addAnnotation(pin)


    }

    private func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }



    private func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        print("annotation title == \(String(describing: view.annotation?.title!))")
    }


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

        print("ok")
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
        annotationView.image = UIImage(named:"setuppin")
        annotationView.canShowCallout = true
        return annotationView

    }

    func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
print(" map has finished")
    }


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

        print("ok")
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customannotation")
        annotationView.image = UIImage(named:"setuppin")
        annotationView.canShowCallout = true
        return annotationView

    }







}

class customPin: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
        self.title = pinTitle
        self.subtitle = pinSubTitle
        self.coordinate = location
    }
}

Upvotes: 1

Views: 771

Answers (3)

giorgio
giorgio

Reputation: 105

DAMN!!!!!

I solved it.... the warning was cause my class was declared ad public and delegate need to be too.

I removed the public statement from my class and everything start to work .

so problem in end was main class declared as public !

Upvotes: 0

giorgio
giorgio

Reputation: 105

this was cause Xcode was raising this warning

instance method 'mapView(:viewFor:)' nearly matches optional requirement 'mapView(:viewFor:)' of protocol 'MKMapViewDelegate'

and suggest make it private as fix. btw even without private not raise anything

Upvotes: 0

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

private is not necessary. Replace

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

with

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

Upvotes: 0

Related Questions