ios - 用于注释的 Swift 不同图像

标签 ios swift mkmapview mkannotationview mkpointannotation

我设法在 Swift 中为注释图钉获得自定义图标,但现在我仍然无法使用 2 个不同的图像来表示不同的注释。现在,一个按钮向 map 添加注释。应该有另一个按钮也添加了注释,但带有另一个图标。

有没有办法为此使用 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
}

最佳答案

viewForAnnotation 委托(delegate)方法中,根据调用该方法的annotation 设置image

一定要 View 出列或创建之后执行此操作(不仅是在 if anView == nil 部分)。否则,使用出队 View 的注释将显示之前使用该 View 的注释的图像。

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

更好的方法是使用实​​现MKAnnotation 协议(protocol)的自定义注释类(一种简单的方法是继承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属性设置 View 的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
}

关于ios - 用于注释的 Swift 不同图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631410/

相关文章:

ios - 在多个线程上访问 ViewContext 导致崩溃

iphone - 如何在 iOS 中翻转视频帧

swift - swift 3 中的类和结构之间的主要区别是什么?

swift - 调整大小的图像周围的空白

iphone - 在单例中设置字典导致 EXC_BAD_ACCESS

ios - 当用户移动 map 时阻止 MKMapView 移动到用户的当前位置

iphone - UIWebview 集成在 Cocos2d 游戏中?

ios - 检测系统警报 View 何时出现/将关闭

ios - 分享 mkmapview 截图

ios - 在 Xcode 的 Interface Builder 中,如何相对于彼此而不是 super View 自动调整大小/空间控件