好的,所以我遇到了这个问题。我想要做的是手动向地图添加多个注释。当我只添加一个注释时,它可以完美地工作。大头针掉了,你可以点击它看它的标注,生活是美好的。
当我想添加多个时,问题就来了。当我添加第二个时,突然引脚的颜色不正确(即根据它们的大小,它们应该是某种颜色,但它们现在都是相同的......),更重要的是,当你点击它们时,看到它们标注时,应用程序因 exex_bad_access 而崩溃。我真的不知道出了什么问题,也许我在地图上添加了太多视图?但它只有 9 个引脚,引脚本身添加得很好。这是我的代码……</p>
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *stops = [[NSMutableArray alloc] init]; //Get list of all the stops available
Bus *bus1 = [[Bus alloc] init]; // Bus 1 holds the stops
stops = [bus1 returnStops];
for (NSString *stop in stops) //Go through each stop to add annotation to map
{
Bus *bus2 = [bus1 initWithStop:stop]; //Create an instance of bus with a given stop
MapAnnotation *eqAnn = [MapAnnotation annotationWithBus:bus2];
[self.mapView addAnnotation:eqAnn]; //Add the annotation to the map
//[eqAnn release];
//[bus2 release];
}
[self recenterMap];
[stops release];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *view = nil;
if(annotation != mapView.userLocation) {
MapAnnotation *eqAnn = (MapAnnotation*)annotation;
view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"busLoc"];
if(nil == view) {
view = [[[MKPinAnnotationView alloc] initWithAnnotation:eqAnn
reuseIdentifier:@"busLoc"] autorelease];
}
CGFloat magnituide = [eqAnn.bus.magnitude floatValue];
if(magnituide >= .80f) {
[(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorRed];
} else if(magnituide >= .60f) {
[(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorPurple];
} else
{
[(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorGreen];
}
[(MKPinAnnotationView *)view setAnimatesDrop:YES];
[view setCanShowCallout:YES];
}
return view;
}
甚至尝试删除第二个功能,但它没有做任何事情。
谢谢您的帮助!PS我还应该补充一点,当您单击注释时,通常会有 9 个引脚中的一两个引脚起作用……</p>
如果我什至尝试在程序中手动手动仅添加两个注释(即删除循环),它仍然失败并且颜色仍然错误......