我正在尝试根据一些标准为MKPinAnnotationView创建三个自定义的引脚图像注释。在本例中,我将迭代自行车站对象的for循环,检查可用自行车的数量是否为0,如果是,则设置一个pin.image,如果不是,则设置另一个pin.image。我还检查docks的数量是否为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发布于 2014-06-08 15:34:50
你不需要在这个方法中遍历所有的自行车站点-它被调用来获得一个特定注解的pin视图。您需要检查与传递给此方法的注释相对应的车站的自行车数量。按照当前代码的编写方式,它将返回适用于数组中所有注释的第一个工作站的视图。
您将注意到的一件事是,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;
}https://stackoverflow.com/questions/24103822
复制相似问题