[英]Change MKAnnotation Pin Color
I have an array of Meetup objects that display on a map as red pins. 我有一个Meetup对象数组,这些对象在地图上显示为红色图钉。 I would like to change the color of the pin based on an attribute of eachhe Meetup object. 我想根据每个Meetup对象的属性更改图钉的颜色。 Each Meetup has a category. 每个聚会都有一个类别。 If the first Meetup in the array has a Category A, I would like the pin to be red, and if the second Meetup has a Category B, I would like the pin to be blue (And so on). 如果阵列中的第一个Meetup具有类别A,我希望该引脚为红色,如果第二个Meetup具有类别B,则希望该引脚为蓝色(依此类推)。 I am trying to do this in viewForAnnotation. 我试图在viewForAnnotation中执行此操作。
Here is some of my code: 这是我的一些代码:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is Meetup {
let identifier = "Location"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as! MKPinAnnotationView!
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.enabled = true
annotationView.canShowCallout = true
annotationView.animatesDrop = false
annotationView.pinTintColor = UIColor(red: 255/255, green: 36/255, blue: 0, alpha: 1)
annotationView.tintColor = UIColor(white: 0.0, alpha: 0.5)
I am trying to do something in here like: 我正在尝试在这里做一些事情:
if annotation is Meetup where Meetup.category == "Category A" {
annotationView.pinTintColor = UIColor(red: 255/255, green: 36/255, blue: 0, alpha: 1)
} else if annotation is Meetup where Meetup.category == "Category B" {
annotationView.pinTintColor = UIColor(red: 0, green: 36/255, blue: 255/255, alpha: 1)
Can someone please point me in the right direction on how to get this accomplished? 有人能为我指出正确的方向吗?
Thanks! 谢谢!
I suppose you are trying to change the pin color by category, You need to use "Switch" the actually code is like this: 我想您正在尝试按类别更改图钉颜色,您需要使用“ Switch”,实际代码如下:
func pinColor(type:String) -> UIColor{
var color = UIColor.purpleColor()
switch category {
case "Category A":
color = UIColor(red: 255/255, green: 36/255, blue: 0, alpha: 1)
case "Category B":
color = UIColor(red: 0, green: 36/255, blue: 255/255, alpha: 1)
default:
color = UIColor.whiteColor()
}
return color
} }
hope this is what you want! 希望这就是你想要的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.