MapBox iOS不同的标记图像? [英] MapBox iOS different marker images?

查看:67
本文介绍了MapBox iOS不同的标记图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以添加ID或其他东西,以便能够设置自定义标记图片?

Is there a way to add an ID or something, that i am able to set custom marker images?

我有多个带数字的标记,并且我需要每个新标记都有另一张图片.

I have multiple markers with numbers, and i need that every new marker has another image.

例如:

marker1-marker_1_image

marker1 - marker_1_image

marker2-marker_2_image

marker2 - marker_2_image

自动取款机我只能为所有标记设置1张图片(全局):

Atm i am only able to set 1 image (globaly) to all markers:

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {

    var annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier("place")

    if annotationImage == nil {
        var image = UIImage(named: "marker_1")!
        image = image.imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, image.size.height/2, 0))
        annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: "place")
    }

    return annotationImage
}

有什么想法吗?还是我可以将MGLAnnotation子类化,并将其用于所有委托方法?

Any ideas? Or can i subclass MGLAnnotation and use that for all delegate methods?

推荐答案

您可以继承并添加userInfo属性(此答案),或者您可以使用现有的标题/字幕属性:

You could subclass and add a userInfo property (as in this answer), or you could use the existing title/subtitle properties:

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    // get the custom reuse identifier for this annotation
    let reuseIdentifier = reuseIdentifierForAnnotation(annotation)
    // try to reuse an existing annotation image, if it exists
    var annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier(reuseIdentifier)

    // if the annotation image hasn‘t been used yet, initialize it here with the reuse identifier
    if annotationImage == nil {
        // lookup the image for this annotation
        let image = imageForAnnotation(annotation)
        annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: reuseIdentifier)
    }

    return annotationImage
}

// create a reuse identifier string by concatenating the annotation coordinate, title, subtitle
func reuseIdentifierForAnnotation(annotation: MGLAnnotation) -> String {
    var reuseIdentifier = "\(annotation.coordinate.latitude),\(annotation.coordinate.longitude)"
    if let title = annotation.title where title != nil {
        reuseIdentifier += title!
    }
    if let subtitle = annotation.subtitle where subtitle != nil {
        reuseIdentifier += subtitle!
    }
    return reuseIdentifier
}

// lookup the image to load by switching on the annotation's title string
func imageForAnnotation(annotation: MGLAnnotation) -> UIImage {
    var imageName = ""
    if let title = annotation.title where title != nil {
        switch title! {
        case "blah":
            imageName = "blahImage"
        default:
            imageName = "defaultImage"
        }
    }
    // ... etc.
    return UIImage(named: imageName)!
}

您将希望使图像加载更加稳定,并自定义重用标识符字符串的特殊性,但这通常应该可以工作.

You’ll want to make the image loading more robust and customize the specificity of the reuse identifier string, but this should generally work.

这篇关于MapBox iOS不同的标记图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆