我有一个存储纬度和经度值的数据库。我想遍历列表并将它们显示在 map 上。我编写了一个条件,表示如果类型是 atm
,则注释颜色应为绿色,如果是 branch
,则注释颜色应显示红色。
if ([myNewArrayElement isEqualToString:@"BRANCH"]) {
self.customAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
NSLog(@"Current coordinates%f%f",[[record valueForKey:@"lat"]floatValue],[[record valueForKey:@"lon"]floatValue]);
mPinColor = @"PURPLE";
self.customAnnotation.title = record.type;
MKPinAnnotationView * annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.customAnnotation
reuseIdentifier:@"CustomAnnotation"] ;
annotationView.canShowCallout = YES;
mPinColor = @"";
NSLog(@"Its Basic");
annotationView.pinColor = MKPinAnnotationColorGreen;
[self.mapView addAnnotation:self.customAnnotation];
self.mapView.delegate = self;
}
else if ([myNewArrayElement isEqualToString:@"ATM"]) {
self.normalAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
mPinColor = @"GREEN";
self.normalAnnotation.title = record.type;
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:self.normalAnnotation
reuseIdentifier:@"NormalAnnotation"] ;
annotationView.canShowCallout = YES;
mPinColor = @"";
NSLog(@"Its Basic");
annotationView.pinColor = MKPinAnnotationColorGreen;
[self.mapView addAnnotation:self.normalAnnotation];
}
我的 viewForAnnotation 是
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
NSLog(@"Length of mpincolor%d",mPinColor.length);
MKPinAnnotationView *annotationView;
if ([mPinColor isEqualToString:@"PURPLE"]) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"] ;
annotationView.canShowCallout = NO;
NSLog(@"Its custom");
annotationView.pinColor = MKPinAnnotationColorPurple;
return annotationView;
}
else if([mPinColor isEqualToString:@"GREEN"]) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"NormalAnnotation"] ;
annotationView.canShowCallout = YES;
mPinColor = @"";
NSLog(@"Its Basic");
annotationView.pinColor = MKPinAnnotationColorGreen;
return annotationView;
}
return nil;
}
我能够正确显示引脚,但颜色显示不正确。我在这里做错了什么?
最佳答案
向 BasicMapAnnotation 类添加 color 属性。在 viewForAnnotation 中查询该属性以正确初始化 pinColor。顺便说一句,您用于在 viewForAnnotation 方法之外创建注释 View 的代码没有多大意义,因为创建的 View 并未被使用。
关于ios - 无法在 MKMapView 中显示多个注释图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982726/