2014-02-25 50 views
0

我有兩個批註數組。 從一個陣列我想要所有的綠色的針腳,從另一個我想要所有的紅色的針腳。 我通過這種方式加入數組:MapView使用differenet顏色添加多個註釋

fromSelectedTab=False; 
[userMap addAnnotations:greenArray]; 
fromSelectedTab=TRUE; 
[userMap addAnnotations:redArray]; 

和viewforannotation我這樣做:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 

if([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 


// static NSString *identifier = @"myAnnotation"; 
// // annotation=(MapObjects*)annotation; 
// MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[userMap dequeueReusableAnnotationViewWithIdentifier:identifier]; 
// if(!annotationView){ 
MKPinAnnotationView * annotationView= [[MKPinAnnotationView alloc] init ];//WithAnnotation:annotation reuseIdentifier:nil]; 
    //annotationView.tintColor=[UIColor blackColor]; 
annotationView.annotation=annotation; 
NSLog(@"flag%d",fromSelectedTab); 
    if (fromSelectedTab==TRUE) { 
     annotationView.pinColor = MKPinAnnotationColorRed; 

    } 
    else{ 
    annotationView.pinColor = MKPinAnnotationColorGreen; 
    // fromSelectedTab=TRUE; 
    } 
    annotationView.animatesDrop = NO; 

    annotationView.canShowCallout = YES; 

//fromSelectedTab=FALSE; 


// else { 
//  annotationView.annotation = annotation; 
// } 

//annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
return annotationView; 
} 

但這種方式我收到相同顏色的針。但我想用兩種顏色。

+0

使用不同的重用標識符n針腳顏色和紅色針腳顏色。並使用dequeueReusableAnnotationViewWithIdentifier:標識符方法也使用initWithAnnotation:reuseIdentifier:方法,並根據註釋爲每種顏色提供不同的重用標識符。 – Sandeep

+0

@瘋狂-36你能告訴我如何? – jayesh

+0

我會在下面的例子中給你看。順便說一句,你不必使用兩個不同的重用標識符。 – Sandeep

回答

2

也實現MKAnnotation協議的自定義類也有機會使用我們自己的變量。我創建了一個名爲redColor的屬性,您可以根據需要命名它。

MyAnnotation.h

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

@interface MyAnnotation : NSObject<MKAnnotation> 

@property (nonatomic, assign, getter = isRedColor) BOOL redColor; 
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString*)title; 


@end 

MyAnnotation.m

#import "MyAnnotation.h" 

@implementation MyAnnotation 

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString*)title{ 
    if(self = [super init]){ 
    _coordinate = coordinate; 
    _title = title; 
    } 
    return self; 
} 

@end 

ViewController.h格力

#import "ViewController.h" 
#import <MapKit/MapKit.h> 
#import "MyAnnotation.h" 

@interface ViewController()<MKMapViewDelegate> 
@property (weak, nonatomic) IBOutlet MKMapView *mapView; 


@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSMutableArray *allAnnotations = [NSMutableArray array]; 
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(50.8500,4.3500); 
    MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"Brussels"]; 
    annotation.redColor = YES; 

    [allAnnotations addObject:annotation]; 


    coordinate = CLLocationCoordinate2DMake(28.61389, 77.20889); 
    annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"New Delhi"]; 
    annotation.redColor = NO; 

    [allAnnotations addObject:annotation]; 


    coordinate = CLLocationCoordinate2DMake(60.1708, 24.9375); 
    annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"Helsinki"]; 
    annotation.redColor = YES; 

    [allAnnotations addObject:annotation]; 

    coordinate = CLLocationCoordinate2DMake(51.5072, 0.1275); 
    annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"London"]; 
    annotation.redColor = NO; 

    [allAnnotations addObject:annotation]; 

    coordinate = CLLocationCoordinate2DMake(39.9139, 116.3917); 
    annotation = [[MyAnnotation alloc] initWithCoordinate:coordinate title:@"Beijing"]; 
    annotation.redColor =YES; 
    [allAnnotations addObject:annotation]; 

    [self.mapView showAnnotations:allAnnotations animated:YES]; 

    [self.mapView addAnnotations:allAnnotations]; 

} 
#pragma mark - MKMapViewDelegate 

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ 
    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PinView"]; 
    if(!pinView){ 
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PinView"]; 
     pinView.canShowCallout = YES; 
    } 
    MyAnnotation *myannotation = (MyAnnotation*)annotation; 
    if(myannotation.redColor) 
    pinView.pinColor = MKPinAnnotationColorRed; 
    else 
    pinView.pinColor = MKPinAnnotationColorGreen; 
    return pinView; 
} 
@end 
+0

在這裏我拿字符串而不是布爾變量 – jayesh

1

這裏的問題是addAnnotations方法可能不同步。這意味着在撥打addAnnotations之後不會立即添加viewForAnnotation委託方法。

解決此問題的正確方法是爲註釋創建類並向其添加顏色屬性。從你的代碼中我們看不到註釋類。

請按照以下步驟解決問題。

  1. 創建實現MKAnnotation協議的類。
  2. 添加一個BOOL屬性fromSelectedTab(或其他任何事情)。
  3. 創建這些類的數組並設置正確的顏色屬性。
  4. 將註釋添加到地圖視圖。
  5. viewForAnnotation方法中,投射註記類並獲取顏色屬性。
  6. 設置MKPinAnnotationViewpinColor屬性基於註記類的屬性。

引腳現在應該是正確的顏色。

另外不要忘記在分配新的MKAnnotationView時使用dequeueReusableAnnotationViewWithIdentifier:方法。

+0

我已經創建了具有三個屬性標題字幕和座標的類。我認爲一樣,但我無法在viewforannotation方法中訪問annotation.title或annotation.subtitle或annotation.coordinate – jayesh

+0

您是否已將id 註釋投射到您的類中? – Legoless

+0

Yupp .... done ....我以爲相同的解決方案,但現在無法施展我搜索如何施展和是的我的功能 – jayesh