2013-10-22 108 views
1

我有一個條件語句來添加地圖註釋圖標/針腳在下面的方法。我遇到的問題是地圖上正在填充所有相同的圖標。它應該檢測到cat id並根據檢測到的cat id顯示圖標。我不確定是什麼問題,因爲這在iOS 6中有效,現在在iOS 7中,地圖僅顯示所有相同的註釋圖標圖像。地圖註釋顯示所有點的所有圖像/針腳

- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation { 
annView = nil; 
if(annotation != mapingView.userLocation) 
{ 

    static NSString *defaultPinID = @""; 
    annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (annView == nil) 
     annView = [[MKAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:defaultPinID] ; 


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 

    annView.rightCalloutAccessoryView = rightButton; 

    MyAnnotation* annotation= [MyAnnotation new]; 

    annotation.catMapId = categoryIdNumber; 
    NSLog(@"categoryIdNumber %@",categoryIdNumber); 
    NSLog(@"annotation.catMapId %@",annotation.catMapId); 


     if (annotation.catMapId == [NSNumber numberWithInt:9]) { 
      annView.image = [UIImage imageNamed:@"PIN_comprare.png"]; 

      NSLog(@"annview 9"); 

     } 

     else if (annotation.catMapId == [NSNumber numberWithInt:10]) { 
      annView.image = [UIImage imageNamed:@"PIN_mangiare.png"]; 

      NSLog(@"annview 10"); 

     } 

     else if (annotation.catMapId == [NSNumber numberWithInt:11]) { 
      annView.image = [UIImage imageNamed:@"PIN_visitare.png"]; 

      NSLog(@"annview 11"); 

     } 

     else if (annotation.catMapId == [NSNumber numberWithInt:12]) { 
      annView.image = [UIImage imageNamed:@"PIN_vivere.png"]; 

      NSLog(@"annview 12"); 

     } 

    annView.canShowCallout = YES; 

} 

return annView; 

}

enter image description here

+0

http://stackoverflow.com/questions/10898122/map-view-annotations-with-different-pin-colors 工程! – user2883540

回答

1

如果像你說的, 「這沒有工作在iOS 6中」,你應該考慮它相當於幸運的是,它確實(或似乎),並且這種設置註釋圖像的方法不應該依賴於任何版本。

儘管@Ar Ma是正確的,應該設置註解視圖的annotation屬性(萬一視圖正在被重用),這並不能解決主要問題。

註釋視圖的image基於的categoryIdNumber這似乎是一些變量viewForAnnotation委託方法的值被設置。

不能假設:

  1. viewForAnnotation會打電話addAnnotation之後立即調用。即使在iOS 6或更早的版本中,這也不能保證。
  2. viewForAnnotation對於每個註釋只會被調用一次。對於與用戶平移或縮放地圖相同的註釋,可以多次調用委託方法,並將註釋返回到屏幕上。
  3. viewForAnnotation將按照添加註釋的順序調用。這是點1的結果和2

我假設你叫addAnnotation之前,該categoryIdNumber設置正確,然後根據上面的不正確的假設,viewForAnnotation使用categoryIdNumber設置圖像。

正在發生的事情是,viewForAnnotation正在被之後的某個全部或部分addAnnotation調用的地圖視圖稱爲完成在這一點categoryIdNumber可能是添加了最後的註解連接的價值和所有註釋使用應用圖像到最後一個註釋。


爲了解決這個問題(的iOS版本,無論),你必須把正確的categoryIdNumber值到每個註釋對象調用addAnnotation之前。

它看起來像您的註釋類是MyAnnotation,並且您已經有一個catMapId屬性。不viewForAnnotation方法,它是爲時已晚 -

您必須調用addAnnotation前的註釋設置該屬性。 (順便說一下,你正在創建一個MyAnnotation對象viewForAnnotation方法,它是沒有意義的。)


那麼,您創建並添加註釋(不viewForAnnotation):

MyAnnotation* myAnn = [[MyAnnotation alloc] init]; 
myAnn.coordinate = ... 
myAnn.title = ... 
myAnn.catMapId = categoryIdNumber; // <-- set catMapId BEFORE addAnnotation 
[mapView addAnnotation:myAnn]; 

然後在viewForAnnotation代碼應該是這樣的:

- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    annView = nil; 
    if(annotation != mapingView.userLocation) 
    { 

     static NSString *defaultPinID = @"MyAnnId"; 
     annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (annView == nil) 
     { 
      annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ; 
      annView.canShowCallout = YES; 
     } 
     else 
     { 
      //view is being re-used, re-set annotation to current... 
      annView.annotation = annotation; 
     } 

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 

     annView.rightCalloutAccessoryView = rightButton; 


     //Make sure we have a MyAnnotation-type annotation 
     if ([annotation isKindOfClass:[MyAnnotation class]]) 
     { 
      //Do not CREATE a local MyAnnotation object here. 
      //Instead, get the catMapId from the annotation object 
      //that was PASSED INTO the delegate method. 
      //MyAnnotation* annotation= [MyAnnotation new]; 
      //annotation.catMapId = categoryIdNumber; 

      MyAnnotation *myAnn = (MyAnnotation *)annotation; 

      //The value of the external variable categoryIdNumber is irrelevant here. 
      //NSLog(@"categoryIdNumber %@",categoryIdNumber); 

      NSLog(@"myAnn.catMapId %@",myAnn.catMapId); 


      //Put the NSNumber value into an int to simplify the code below. 
      int myAnnCatMapId = [myAnn.catMapId intValue]; 

      NSString *imageName = nil; 
      switch (myAnnCatMapId) 
      { 
       case 9: 
       { 
        imageName = @"PIN_comprare.png"; 
        break; 
       } 

       case 10: 
       { 
        imageName = @"PIN_mangiare.png"; 
        break; 
       } 

       case 11: 
       { 
        imageName = @"PIN_mangiare.png"; 
        break; 
       } 

       case 12: 
       { 
        imageName = @"PIN_vivere.png"; 
        break; 
       } 

       default: 
       { 
        //set some default image for unknown cat ids... 
        imageName = @"default.png"; 
        break; 
       } 
      } 

      annView.image = [UIImage imageNamed:imageName]; 

      NSLog(@"annview %d", myAnnCatMapId); 
     } 
    } 

    return annView; 
} 
+0

這工作得很好!感謝您的時間幫助我:) – user2588945

+0

不客氣。您可能已經解決了這個問題,但是我在switch語句中犯了一個小錯誤。對於案例11,它應該是visitare的形象。 – Anna

1

末加入這一行:

annView.annotation = annotation; 
+0

謝謝。註釋確實顯示,但它們都是相同的針圖像。它應該是針對不同類別ID的不同針圖像。 – user2588945

+0

我添加了一個顯示引腳的屏幕截圖,但它們都是相同的引腳,並且應該根據條件各不相同。 – user2588945

0

同樣的麻煩,我的決定是不要使用

pinView.animatesDrop = YES; 

自定義圖標只是不適用於我的動畫下降。

相關問題