即使设置了委托也不会调用MKMapview委托

时间:2019-06-14 01:14:46

标签: swift mkmapview xcode4.2 mkmapviewdelegate

我有一个集成滚动视图的UIView。在滚动视图内部,我有一个地图,用户需要在其中移动图钉。稍后我将重用图钉的位置。

我正在为地图设置强引用并设置委托(设计和代码)。我能够看到创建的图钉,但是具有自定义图标并可以将其设置为可拖动,因此我实现了mapView(viewFor MKAnnotation

这似乎从来没有被提及过,我不明白为什么。

我还尝试通过didfinishloading来获取另一位委托,以了解问题是否与mkannotation有关,但是从来没有调用过该问题。

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
    }
}

3 个答案:

答案 0 :(得分:0)

private是不必要的。替换

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

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

答案 1 :(得分:0)

这是Xcode发出此警告的原因

实例方法'mapView(:viewFor :)'与协议'MKMapViewDelegate'的可选要求'mapView(:viewFor :)'几乎匹配

并建议将其设置为私有。顺便说一句,即使没有私人也不会筹集任何东西

答案 2 :(得分:0)

DAMN !!!!!

我解决了它。...警告是因为我的课程被宣布为公共广告,因此委托也必须如此。

我从课堂上删除了公开声明,一切开始正常工作。

所以最终的问题是主类被宣布为公开!