MKMapView 中同一张地图上的多种图钉颜色
我的应用程序中有一个 MKMapView,上面有多个图钉,我想为每个图钉设置不同的颜色。我的视图控制器正在实现 MKMapViewDelegate ,并且我已经定义了 viewForAnnotation 方法。
- (MKAnnotationView *) mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"pin"];
annView.pinColor = MKPinAnnotationColorGreen;
return annView;
}
它工作正常并将引脚颜色更改为绿色。然而,所有引脚的颜色都发生了变化,我想用几种颜色给它们着色(根据我定义的一些标准,假设我想要奇数引脚为绿色,偶数引脚为黄色或像这样简单的东西)。如何才能实现这一目标?
I have a MKMapView
in my app with several pins on it and I'd like to set different colors for each pin. My view controller is implementing MKMapViewDelegate
and I've defined viewForAnnotation method.
- (MKAnnotationView *) mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"pin"];
annView.pinColor = MKPinAnnotationColorGreen;
return annView;
}
It works fine and changes pin color to green. However the color is changed for all pins and I'd like to color them with several colors (based on some criteria I'd define, lets assume I want to have odd pins green and even pins yellow or something as simple as that). How can this be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过使用图像而不是 pinColor 解决了这个问题。这样我就可以拥有任意数量的引脚。
I've solved this issue by using images instead of pinColor. This way I can have as many pins as I want.
就这么简单吗?
Something as simple as that?
我遇到了同样的问题,然后我使用此代码解决了
I have met the same issue then I solved by using this code