MKMapView 中同一张地图上的多种图钉颜色

发布于 2024-09-16 15:45:34 字数 555 浏览 3 评论 0原文

我的应用程序中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-09-23 15:45:34

我通过使用图像而不是 pinColor 解决了这个问题。这样我就可以拥有任意数量的引脚。

I've solved this issue by using images instead of pinColor. This way I can have as many pins as I want.

油焖大侠 2024-09-23 15:45:34
if(annotation.fillsYourCriteria)  
    annView.pinColor = MKPinAnnotationColorGreen;  
else  
    annView.pinColor = MKPinAnnotationColorYellow;  
return annView;  

就这么简单吗?

if(annotation.fillsYourCriteria)  
    annView.pinColor = MKPinAnnotationColorGreen;  
else  
    annView.pinColor = MKPinAnnotationColorYellow;  
return annView;  

Something as simple as that?

风月客 2024-09-23 15:45:34

我遇到了同样的问题,然后我使用此代码解决了

if([[pinView.annotation title] isEqualToString:@"Current Location"])
{
    pinView.pinColor = MKPinAnnotationColorRed; 
}
else
{
    pinView.pinColor = MKPinAnnotationColorPurple; 
}

I have met the same issue then I solved by using this code

if([[pinView.annotation title] isEqualToString:@"Current Location"])
{
    pinView.pinColor = MKPinAnnotationColorRed; 
}
else
{
    pinView.pinColor = MKPinAnnotationColorPurple; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文