2013-07-30 28 views
0

我想要兩種顏色的註釋。爲此,我使用了以下代碼,mapkit中的iOS引腳顏色問題

- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation {  
   static NSString *identifier = @"MyLocation"; 
   if ([annotation isKindOfClass:[PlaceMark class]]) { 
       MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
       annotationView.enabled = YES; 
       annotationView.canShowCallout = YES; 
       @try { 
           if (annotationView == nil) { 
               annotationView = [[MKPinAnnotationView alloc] 
                                 initWithAnnotation:annotation 
                                 reuseIdentifier:identifier]; 
           } else { 
               annotationView.annotation = annotation; 
                
               if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"]) 
               { 
                   annotationView.pinColor = MKPinAnnotationColorRed; 
               } 
               else 
               { 
                   annotationView.pinColor = MKPinAnnotationColorGreen; 
               } 
           } 
       } 
       @catch (NSException *exception) { 
           NSLog(@"nsCenterOrOffice exception = %@",exception); 
       } 
       return annotationView; 
   } 
   return nil; 
} 

但是我仍然無法爲所需的註釋設置所需的顏色。有時特定的註釋針顏色是紅色的,有時是綠色的。我不明白爲什麼會發生這種情況。有誰能夠幫助我 ?謝謝...

我對我的code..this返工是我更新的代碼

MapAnnotation.h

#import <MapKit/MapKit.h> 
    #import <Foundation/Foundation.h> 

    @interface MapAnnotation : MKPointAnnotation 
    { 
       NSString *title; 
       NSString *subtitle; 
       int dealLnk; 
       float latitude; 
       float longitude; 
        
       CLLocationCoordinate2D coordinate; 
    } 

    @property (nonatomic, copy) NSString *title; 
    @property (nonatomic, copy) NSString *subtitle; 
    @property (nonatomic, assign) int dealLnk; 
    @property (nonatomic, assign) float latitude; 
    @property (nonatomic, assign) float longitude; 
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

    - (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(int)dealLnk latitude:(float)latitude longitude:(float)longitude andCoordinate:(CLLocationCoordinate2D)c2d; 
@end 

MapAnnotation.m

#import "MapAnnotation.h" 

@implementation MapAnnotation 

@synthesize title,subtitle,dealLnk,coordinate,latitude,longitude; 

- (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(int)z latitude:(float)latitude1 longitude:(float)longitude1 andCoordinate:(CLLocationCoordinate2D)c2d { 
   title = ttl; 
   subtitle = subttl; 
   dealLnk =z; 
   coordinate = c2d; 
   longitude = longitude1; 
   latitude = latitude1; 
    
   return self; 
} 
@end 

這是我實現文件

-(void)startAddingAnnotation 
{ 
   @try { 
       CLLocationCoordinate2D annotationCoord; 
        
       z=0; 
       for (int i=0; i < [nslatitude count] ; i++,z++) 
       { 
            
           MapAnnotation  *dealAnnotation   = [[MapAnnotation  alloc] init]; 
            
           dealAnnotation.dealLnk = z; 
           annotationCoord.latitude =  (CGFloat) [[nslatitude objectAtIndex:i] floatValue]; 
           annotationCoord.longitude = (CGFloat)[[nslongitude objectAtIndex:i] floatValue]; 
           dealAnnotation.coordinate = annotationCoord; 
            
           dealAnnotation.title = [nsCenterName objectAtIndex:i]; 
           dealAnnotation.subtitle = [nsCenterAddress objectAtIndex:i]; 
            
           NSLog(@"latitude = %f",dealAnnotation.latitude); 
           NSLog(@"longitude = %f",dealAnnotation.longitude); 
            
           NSLog(@"dealAnnotation.dealLnk = %d",dealAnnotation.dealLnk); 
           NSLog(@"dz = %d",z); 
           [mapView addAnnotation:dealAnnotation]; 
          
       } 
        
   } 
   @catch (NSException *exception) { 
        
       NSLog(@"Exception = %@",exception); 
   } 
    
   cord.longitude = -112.05186; 
   cord.latitude = 33.46577; 
    
   MKCoordinateRegion region; 
   region.center = cord; 
    
   MKCoordinateSpan span = {.latitudeDelta = 1.0, .longitudeDelta = 1.0}; 
   region.span = span; 
    
   [mapView setRegion:region]; 
    
    
} 

#pragma mark - MapView_Delegate 
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation { 
    

   MapAnnotation *annotation1 = (MapAnnotation *)annotation; 
   z = annotation1.dealLnk; 
    
     NSLog(@"annotation1.longitude = %f",annotation1.latitude); 
     NSLog(@"annotation1.longitude = %f",annotation1.longitude); 
    
   if(z<[nslatitude count]) 
   { 
       MKAnnotationView *pinView = nil; 
       static NSString *defaultPinID = @"pin1"; 
        
       pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
        
      
       if (pinView == nil) 
           pinView = [[MKAnnotationView alloc] 
                      initWithAnnotation:annotation1 reuseIdentifier:defaultPinID]; 
       pinView.canShowCallout = YES; 
        
       @try { 
            
           if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"]) 
           { 
               pinView.image = [UIImage imageNamed:@"flag1.png"]; 
           } 
           else 
           { 
               pinView.image = [UIImage imageNamed:@"flag2.png"]; 
           } 
           pinView.annotation = annotation1; 
           pinView.tag = [[nsCenterName objectAtIndex:z] intValue]; 
            
       } 
       @catch (NSException *exception) { 
           NSLog(@"nsCenterOrOffice exception = %@",exception); 
       } 
        
       UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
       [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
       [pinView setRightCalloutAccessoryView:rightButton]; 

       return pinView; 
   } 
   else 
   { 
       return nil; 
   } 
    
} 

現在的問題是,所有的註釋都指向一些不同的位置。我不明白爲什麼會發生這種情況?我的代碼中是否有任何錯誤?

+0

你檢查了你的條件** [[nsCenterOrOffice objectAtIndex:z] isEqualToString:@「C」] **是否滿足? – Pratik

+0

是的..這種情況正在起作用。該聲明中沒有問題。 – user2260521

+0

然後你可以使用圖像針腳 – Pratik

回答

1

確定引腳顏色的條件是基於此委託方法外部的外部數據,並且不能保證它將與爲特定註釋調用此委託方法時同步。


在這種if條件:

if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"]) 

z變量是顯然聲明和設置一些實例變量(可能是正確的添加註解之前)此委託方法之外。

再次,有不保證代表方法將被添加註釋後立即調用。也不能保證每個註釋只能調用一次委託方法。 因此,z的值可能不對應於正在調用委託方法的當前annotation

這是非常可能的addAnnotation之後被稱爲委託方法以及被調用時,它會以不同的順序比加入註釋被調用,並且它將被稱爲用於每個註釋多次(例如如果用戶平移或縮放地圖並且註釋回到視圖中)。


爲了解決這個問題,添加「爲中心或辦公室」屬性設置爲Placemark類本身和調用addAnnotation之前創建的註釋和時設置該屬性。然後,在viewForAnnotation方法中,您可以將annotation參數強制轉換爲Placemark並訪問該屬性以實現該條件。例如:

//where you create and add the annotation: 
Placemark *pm = [[Placemark alloc... 
pm.coordinate = ... 
pm.title = ... 
pm.centerOrOffice = [nsCenterOrOffice objectAtIndex:z]; 
[mapView addAnnotation:pm]; 


//in viewForAnnotation: 
Placemark *myPlacemark = (Placemark *)annotation; 
if ([myPlacemark.centerOrOffice isEqualToString:@"C"]) 
... 


一個獨立的,不相關的問題是,註釋視圖創建邏輯是不完全正確。
代碼調用initWithAnnotation兩次,實際上第二個initWithAnnotation永遠不會被調用,因爲第一次調用將始終成功。

你可能想要的是首先撥打dequeueReusableAnnotationViewWithIdentifier:然後,如果返回nil,請致電initWithAnnotation

viewForAnnotation重組代碼,一定要把那臺註解視圖的pinColor出列/初始化代碼(如前return annotationView;右)妥善處理再利用的意見。

0

您的viewForAnnotation依賴於一些外部變量,nsCenterOrOfficez。直到你向我們展示你如何設置這些變量,我們當然無法幫到你。

無論如何,這不是一種謹慎的做法。您的viewForAnnotation通常應該僅僅依靠annotation的屬性,而不是外部變量。

+0

謝謝。我編輯了我的問題。請檢查。 – user2260521

+0

@ user2260521修改'viewForAnnotation'看起來更有希望。這裏有很多問題,具有不同程度的意義(唯一明顯的錯誤是'initWithTitle'不會調用'[super init]',但是再次,你不使用這種方法,所以這不太可能是問題的根源)。如果您的問題是添加了註釋,但只是錯誤的圖標,則應該在'viewForAnnotation'中記錄'[nsCenterOrOffice objectAtIndex:z]'。如果您的問題是其他問題,請澄清目前的問題。 – Rob