我正在尝试根据一些条件为MKPinAnnotationView创建三个自定义图钉图像注释。在这种情况下,我遍历自行车站对象的for循环,检查可用自行车的数量是否为0,如果是,则设置一个pin.image,如果不是,则设置另一个pin.image。我还要检查停靠点的数量是否为0,并相应地设置另一个图像。我知道,在某些情况下,第一个if语句的计算结果为YES,但是所有的pin注释都与语句的else部分中表示的图像有关。我想我对在哪里实例化MKAnnotationView并返回它感到困惑,因为我的所有固定图像都不会像现在一样返回相同的图像。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
for (DivvyStation *divvyStation in self.divvyStations) {
if (divvyStation.availableBikes.intValue < 1) {
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"nobikes"];
return pin; }
else if (divvyStation.availableDocks.intValue < 1) {
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"dock"];
return pin;}
else {
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"Divvy-FB"];
return pin;
}
}
return nil;
}
好的,我认为我了解问题所在,但是我的针脚显示默认的红色,因此我认为自定义类没有正确采用MKAnnotation协议。我的自定义类.h文件在下面。我读到MKAnnotation协议必须实现一个我添加的坐标属性。有什么我想念的吗?
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface DivvyStation : NSObject <MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property NSNumber *identifier;
@property NSString *stationName;
@property NSNumber *availableDocks;
@property NSNumber *totalDocks;
@property NSNumber *latitude;
@property NSNumber *longitude;
@property NSString *statusValue;
@property NSNumber *statusKey;
@property NSNumber *availableBikes;
@property NSString *streetAddress1;
@property NSString *streetAddress2;
@property NSString *city;
@property NSString *postalCode;
@property NSString *location;
@property NSString *landMark;
@end
最佳答案
您不需要使用这种方法来遍历所有的自行车站-调用它是为了获取特定注释的针脚视图。您需要检查与已传递给此方法的注释对应的车站的自行车数量。当前编写代码的方式将返回适用于数组中所有注释的第一个工作站的视图。
您会注意到的一件事是,MKAnnotation是一个协议,而不是您需要子类化的对象。这意味着您可以在数据模型中使现有对象采用MKAnnotation协议。
在这种情况下,您可以使DivvyStation类采用MKAnnotation协议。然后,这将是您的viewForAnnotation方法。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
if ([annotation isKindOfClass:[DivvyStation class]]) {
DivvyStation *divvyStation=(DivvyStation *)annotation
if (divvyStation.availableBikes.intValue < 1) {
MKAnnotationView *pin = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"nobikes"];
return pin; }
else if (divvyStation.availableDocks.intValue < 1) {
MKAnnotationView *pin = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"dock"];
return pin;}
else {
MKAnnotationView *pin = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"Divvy-FB"];
return pin;
}
}
return nil;
}
关于ios - 设置自定义MKPinAnnotationView图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24103822/