在 mapkit 中显示自定义注释

Posted

技术标签:

【中文标题】在 mapkit 中显示自定义注释【英文标题】:show custom annotation in mapkit 【发布时间】:2017-02-04 20:27:38 【问题描述】:

我正在尝试在 mapkit 上显示图像注释而不是图钉注释。我正在使用此代码:

import UIKit
import MapKit

class ViewController2: UIViewController , MKMapViewDelegate  

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() 
        super.viewDidLoad()

        mapView.delegate = self

        let coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)

        let region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        mapView.setRegion(region, animated: true)



        var info1 = CustomPointAnnotation()
        info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)
        info1.title = "Info1"
        info1.subtitle = "Subtitle"
        info1.imageName = "taxi"

        var info2 = CustomPointAnnotation()
        info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)
        info2.title = "Info2"
        info2.subtitle = "Subtitle"
        info2.imageName = "smile"

        mapView.addAnnotation(info1)
        mapView.addAnnotation(info2)

        mapView.showAnnotations(mapView.annotations, animated: true)
    

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

        print("delegate called")

        if !(annotation is CustomPointAnnotation) 
            return nil
        

        let reuseId = "test"

        var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if anView == nil 
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            anView?.canShowCallout = true
        
        else 
            anView?.annotation = annotation
        

        //Set annotation-specific properties **AFTER**
        //the view is dequeued or created...

        let cpa = annotation as! CustomPointAnnotation
        anView?.image = UIImage(named:cpa.imageName)

        return anView
    


    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    override func viewDidAppear(_ animated: Bool) 

    




class CustomPointAnnotation: MKPointAnnotation 
    var imageName: String!

我认为这个方法没有调用viewForAnnotation,尽管我将代理连接到viewcontroller : mapView.delegate = self

我仍然在地图上看到 pin 注释而不是 custominnotation

【问题讨论】:

ios Swift MapKit Custom Annotation的可能重复 如果您熟悉设置断点和跟踪代码,请查看代码何时到达您的 viewForAnnotation 委托,它是否通过了“如果!(注解为 CustomPointAnnotation)”或返回。跨度> 【参考方案1】:

现在何时实现此代码:

import UIKit
import MapKit

class ViewController2: UIViewController , MKMapViewDelegate  

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() 
        super.viewDidLoad()

        mapView.delegate = self

        let coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)

        let region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

        mapView.setRegion(region, animated: true)



        let annotation = MKPointAnnotation()
        annotation.title = "Annotation Created"
        annotation.subtitle = "mahdi"
        annotation.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)

        let annotation2 = MKPointAnnotation()
        annotation2.title = "Annotation Created"
        annotation2.subtitle = "mahdi"
        annotation2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)



        mapView.addAnnotation(annotation)
        mapView.addAnnotation(annotation2)

    


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

        guard !annotation.isKind(of: MKUserLocation.self) else 

            return nil
        

        let annotationIdentifier = "AnnotationIdentifier"

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)

        if annotationView == nil 
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            annotationView!.canShowCallout = true
        
        else 
            annotationView!.annotation = annotation
        

        annotationView!.image = UIImage(named: "taxi")

        return annotationView

    

我可以看到这样的两个图像注释:

enter image description here

现在我想将此图像包含在用户位置中,我尝试使用此代码但仍然看到红色引脚注释而不是图像

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 


        if let location = manager.location?.coordinate 

            userLocation  = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)

           // print(userLocation)

            if driverOnTheWay == false 

                let region = MKCoordinateRegion(center: userLocation, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
                self.mapView.setRegion(region, animated: true)
                self.mapView.removeAnnotations(self.mapView.annotations)


                let annotation = MKPointAnnotation()
                annotation.title = "مكانك هنا"
                annotation.subtitle = "mahdi"
                annotation.coordinate = userLocation
                self.mapView.addAnnotation(annotation)




            

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

                guard !annotation.isKind(of: MKUserLocation.self) else 

                    return nil
                

                let annotationIdentifier = "AnnotationIdentifier"

                var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)

                if annotationView == nil 
                    annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
                    annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
                    annotationView!.canShowCallout = true
                
                else 
                    annotationView!.annotation = annotation
                

                annotationView!.image = UIImage(named: "car")

                return annotationView

            

如何显示带有用户位置的汽车图像

【讨论】:

以上是关于在 mapkit 中显示自定义注释的主要内容,如果未能解决你的问题,请参考以下文章

自定义注释苹果mapkit

MapKit 自定义注释被添加到地图,但在地图上不可见

iPhone Mapkit 将自定义图像和引脚添加到注释中

地图注释显示不正确的自定义图钉图像

iOS 如何让 MapKit 显示自定义室内地图?

MapKit iPhone 开发;当前位置和注释