2014-02-25 46 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