【问题标题】:MKMapview delegate never called even with set delegate即使设置了委托,MKMapview 委托也从未调用过
【发布时间】:2019-10-28 14:58:59
【问题描述】:

我有一个集成了滚动视图的 UIView。在滚动视图中,我有一张用户需要移动图钉的地图。稍后我将重复使用 pin 的位置。

我正在为地图设置强参考和设置委托(设计和代码)。我能够看到创建的图钉,但有自定义图标和将其设置为可拖动的可能性,我已经实现了 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
    }
}

【问题讨论】:

    标签: ios swift mkmapview xcode4.2 mkmapviewdelegate


    【解决方案1】:

    private 不是必需的。替换

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

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

    【讨论】:

      【解决方案2】:

      这是因为 Xcode 发出了这个警告

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

      并建议将其设为私有作为修复。顺便说一句,即使没有私人也不会提出任何东西

      【讨论】:

        【解决方案3】:

        该死的!!!!!!

        我解决了....警告是因为我的班级被宣布为公开的,而代表也需要公开。

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

        所以问题最终是主类声明为公共!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多