【问题标题】:MapView Add mulitple annottion with differenet colorsMapView 添加多个不同颜色的注解
【发布时间】:2014-02-25 09:26:18
【问题描述】:

我有两个注释数组。 从一个阵列中,我想要所有绿色引脚,而从另一个阵列中,我想要所有红色引脚。 我正在通过这种方式添加数组:

 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;
 }

但是这样我得到了相同颜色的别针。但我想要两种颜色。

【问题讨论】:

  • 对绿色引脚颜色和红色引脚颜色使用不同的重用标识符。并且使用 dequeueReusableAnnotationViewWithIdentifier:identifier 方法也使用 initWithAnnotation:reuseIdentifier: 方法并根据注解为每种颜色赋予不同的reuseidentifier。
  • @insane-36 你能告诉我怎么做吗???
  • 我将在下面的示例中向您展示。顺便说一句,您不必使用两个不同的重用标识符。
  • @insane-36...例子在哪里?
  • 您好,您解决了这个问题吗?我已经向您展示了下面的示例。

标签: ios iphone mkmapview mkannotation mkannotationview


【解决方案1】:

同样实现 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

【讨论】:

  • 这里我用的是字符串而不是布尔变量
【解决方案2】:

这里的问题是addAnnotations 方法可能不是同步的。这意味着viewForAnnotation 委托方法不会在调用addAnnotations 之后立即添加。

解决这个问题的正确方法是为注解创建一个类并向它添加颜色属性。从您的代码中我们看不到注释类。

按照以下步骤解决问题。

  1. 创建一个实现MKAnnotation 协议的类。
  2. 添加一个名为 fromSelectedTabBOOL 属性(或其他任何内容)。
  3. 创建这些类的数组并设置正确的颜色属性。
  4. 为地图视图添加注释。
  5. viewForAnnotation方法中,转换注解类并获取color属性。
  6. 根据注解类的属性设置MKPinAnnotationViewpinColor属性。

图钉现在应该是正确的颜色了。

在分配新的 MKAnnotationView 时不要忘记使用dequeueReusableAnnotationViewWithIdentifier: 方法。

【讨论】:

  • 我已经创建了具有三个属性标题字幕和坐标的类。我也这么想,但我无法在 viewforannotation 方法中访问 annotation.title 或 annotation.subtitle 或 annotation.coordinate
  • 您是否将 id注释添加到您的班级?
  • Yupp....完成....我认为相同的解决方案,但现在无法投射我搜索如何投射,是的,我的功能已完成
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-11
相关资源
最近更新 更多