【问题标题】:Custom Map Pin Image Objective-C自定义地图图钉图像 Objective-C
【发布时间】:2015-12-05 14:32:28
【问题描述】:

我目前正在努力使用自定义地图图钉图像而不是默认的彩色图钉。我尝试了一些在线指南,但似乎无法将其集成到我当前的设置中。

目前我在特定时间(在线)设置了某种颜色(着色)的大头针,当时间超出这些参数时,大头针会变成不同的颜色。

我想将这些图钉更改为我创建的两个不同的自定义图钉图像;一个在线,一个离线。

我已经阅读了一些关于 MKPinAnnotationView 不喜欢 pinView.image 属性的内容(我已经尝试过并且可以确认它不起作用)。

如果有人可以编辑此代码,以便我可以为注释标记 0 生成“online.png”图像,为注释标记 1 生成“offline.png”图像,那将是完美的。

MapViewController.m

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

pinView.rightCalloutAccessoryView = advertButton;
pinView.canShowCallout = YES;
pinView.draggable = NO;
pinView.highlighted = YES;

if (annotation == mapView.userLocation)
    return nil;

if (([(Annotation*)annotation tag] == 0) && [[(Annotation*)annotation area] isEqualToString:@"Online"])

{
    pinView.pinTintColor = [UIColor colorWithRed:0.92 green:0.21 blue:0.21 alpha:1];
    pinView.alpha = 1;
    [advertButton addTarget:self action:@selector(Online:) forControlEvents:UIControlEventTouchUpInside];
}

else if (([(Annotation*)annotation tag] == 1) && [[(Annotation*)annotation area] isEqualToString:@"Offline"])

{
    pinView.pinTintColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5];
    pinView.alpha = 1;
    [advertButton addTarget:self action:@selector(Offline:) forControlEvents:UIControlEventTouchUpInside];

}   
return pinView;
}

任何帮助将不胜感激;我不是天生的编码员,所以放轻松。

干杯

刘易斯

【问题讨论】:

    标签: objective-c image mkannotation mkannotationview mkpinannotationview


    【解决方案1】:

    如果您想将图钉显示为注释,请使用MKPinAnnotationView。如果要使用自定义注释图像,则必须使用MKAnnotationView,并设置image 属性。
    所以我建议更换

    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];  
    

    MKAnnotationView *pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];  
    

    并设置image 属性,例如使用:

    pinView.image = [UIImage imageNamed:@„xxx“];  
    

    当然,你应该适当地重命名pinViewCustomPinAnnotationViewxxx

    【讨论】:

    • 你节省了我的时间。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    相关资源
    最近更新 更多