2009-08-17 60 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:]添加要存儲的每個停止點。

之後呢?目前還不清楚你真的想做什麼。