2009-08-17 90 views
1

好的,所以我遇到了这个问题。我想要做的是手动添加多个注释到地图。当我只添加一个注释时,它完美地工作。销滴,你可以点击它看到它的标注,生活是美好的。将多个注释添加到地图时出现问题

问题来了,当我想添加多个。当我添加第二个时,突然针的着色不正确(即取决于它们的大小,它们应该是某种颜色,但它们现在都是相同的......),更重要的是当你点击它们时,看到它们标注,该应用程序崩溃与exex_bad_access。我真的不知道什么是错的,也许我在地图上添加了太多的视图?但它只有9个引脚,引脚本身也很好。 这里是我的代码...

- (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中,当你点击注释时它会起作用...

如果我甚至尝试在程序中手工操作两个注释(即删除循环) ,它仍然失败,颜色仍然是错误的...

+0

另外,我要补充的是,如果我手动两个注解添加到地图上,以同样的方式的东西崩溃。 总线BUS2 * = [BUS1 initWithStop:[停止objectAtIndex:0]; MapAnnotation * eqAnn = [MapAnnotation annotationWithBus:BUS2]; [self.mapView addAnnotation:eqAnn]; 总线BUS2 * = [BUS1 initWithStop:停止objectAtIndex:1]; MapAnnotation * eqAnn = [MapAnnotation annotationWithBus:bus2]; [self.mapView addAnnotation:eqA NN]; 这仍然失败。 – kodai 2009-08-17 18:53:07

回答

0

所以答案是我一直给bus1发送init对象,所以它感到困惑。 。

“嗨,大卫,

你的数据模型看起来大清洗我你只有一个总线对象,你是重复发送initWithStop:到

希望这有助于。

祝你好运! “

谢谢你们的帮助!你们都帮我不少!

1

看来你的内存管理的stops变量是不正确的。你分配一个可变数组,然后用返回值-[Bus returnStops]替换该数组,然后释放它。也不清楚bus2发生了什么 - -[Bus initWithStop:]是否返回不同Bus的实例?在已经初始化的对象上发送任何方法-init*并不常见。我认为你可能对Cocoa Touch中的内存管理惯例感到困惑。这里有一系列文章和其他参考文献on Cocoa memory management(这是同一个野兽)。

0

您是否尝试过使用AddAnnotations而不是添加注释? - (void)addAnnotations:(NSArray *)注释。这可能适用于你......但看看上面的答案,并进一步检查你的viewDidLoad中有一些内存管理问题(虽然这可能不是你的问题的原因,但它可能是)。首先是分配数组(停止),然后使用Bus对象中的某个数组对其进行ovveriding,这会导致泄漏。此外,您还在释放可能导致崩溃的数组,因为您正在释放实际位于Bus对象中的数组,但未增加引用计数。我不确定initWithStop在干什么,但是如果initWithStop保留了这个对象,你也可能在这里发生泄漏。

0

我不会把它称为内存管理问题 - 我只是说你错误地使用了数组引用。

使用NSMutableArray * stops = [[NSMutableArray alloc] init]构造数组后,下一步是使用[stops addObject:]添加要存储的每个停止点。

之后呢?目前还不清楚你真的想做什么。