【问题标题】:Swift different images for Annotation用于注释的 Swift 不同图像
【发布时间】:2014-10-27 04:36:39
【问题描述】:

我设法在 Swift 中为注释 pin 获取自定义图标,但现在我仍然坚持使用 2 个不同的图像来进行不同的注释。现在,一个按钮将注释添加到地图。应该有另一个按钮,它也添加了注释,但带有另一个图标。

有没有办法为此使用reuseId?

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var Map: MKMapView!

@IBAction func btpressed(sender: AnyObject) {

    var lat:CLLocationDegrees = 40.748708
    var long:CLLocationDegrees = -73.985643
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    Map.setRegion(region, animated: true)


    var information = MKPointAnnotation()
    information.coordinate = location
    information.title = "Test Title!"
    information.subtitle = "Subtitle"

    Map.addAnnotation(information)
}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if !(annotation is MKPointAnnotation) {
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.image = UIImage(named:"1.png")
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }

    return anView
}

【问题讨论】:

    标签: ios swift mkmapview mkannotationview mkpointannotation


    【解决方案1】:

    viewForAnnotation 委托方法中,设置image,基于调用该方法的annotation

    确保在视图出列或创建视图后执行此操作(不仅在if anView == nil 部分中)。否则,使用出队视图的注解将显示之前使用该视图的注解的图像。

    对于基本的MKPointAnnotation,区分注释的一种粗略方法是通过它们的title,但这不是很灵活。

    更好的方法是使用实​​现MKAnnotation 协议的自定义注释类(一个简单的方法是继承MKPointAnnotation)并添加任何需要的属性来帮助实现自定义逻辑。

    在自定义类中,添加一个属性,比如imageName,您可以使用它来根据注释自定义图像。

    这个例子子类MKPointAnnotation

    class CustomPointAnnotation: MKPointAnnotation {
        var imageName: String!
    }
    

    创建CustomPointAnnotation 类型的注解并设置它们的imageName

    var info1 = CustomPointAnnotation()
    info1.coordinate = CLLocationCoordinate2DMake(42, -84)
    info1.title = "Info1"
    info1.subtitle = "Subtitle"
    info1.imageName = "1.png"
    
    var info2 = CustomPointAnnotation()
    info2.coordinate = CLLocationCoordinate2DMake(32, -95)
    info2.title = "Info2"
    info2.subtitle = "Subtitle"
    info2.imageName = "2.png"
    

    viewForAnnotation中,使用imageName属性设置视图的image

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if !(annotation is CustomPointAnnotation) {
            return nil
        }
    
        let reuseId = "test"
    
        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(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
    }
    

    【讨论】:

    • 简单明了+1
    • 请注意,您需要实现协议 'MKMapViewDelegate' 并将您的控制器配置为您的委托(在故事板中或在代码中手动),以便 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 方法工作。这让我有点不知所措。
    • 我为这个问题找到的最佳答案。 +1 顺序和答案的简单性
    • @Anna 我的定位点没有显示在地图上。只有标注在那里。怎么了
    • 嗨@Anna,我的所有点都没有包含在我的UImapview中,它超出了范围......应该是什么情况?
    【解决方案2】:

    在 Anna 和 Fabian Boulegue 的帮助下编写 iOS Swift 代码:

    import UIKit
    import MapKit
    
    class ViewController: UIViewController, MKMapViewDelegate {
    
        @IBOutlet weak var mapView: MKMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.mapView.delegate = self
    
            var info1 = CustomPointAnnotation()
            info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042)
            info1.title = "Info1"
            info1.subtitle = "Subtitle"
            info1.imageName = "flag.png"
    
            var info2 = CustomPointAnnotation()
            info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098)
            info2.title = "Info2"
            info2.subtitle = "Subtitle"
            info2.imageName = "flag.png"
    
            mapView.addAnnotation(info1)
            mapView.addAnnotation(info2)
        }
    
        func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    
            println("delegate called")
    
            if !(annotation is CustomPointAnnotation) {
                return nil
            }
    
            let reuseId = "test"
    
            var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(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.
        }
    }
    
    class CustomPointAnnotation: MKPointAnnotation {
        var imageName: String!
    }
    

    【讨论】:

    • 对于 Swift 3 我不得不改用:func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    • 真的很棒。它节省了我的时间。
    • 需要将注解向下转换为CustomPointAnnotation。将其更改为let pickupCustomAnno = annotation as! CustomPointAnnotation
    • 将reuseId保留为“test”会很糟糕吗?那会带来什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多