Map2: 在地图上添加锚点 添加不同颜色的锚点

在地图视图上标注一些点来特别说明这些地理位置

方案:

使用地图内置的标注。如下步骤:

1.创建一个新的类,命名为 MyAnnotation。
2.确保这个类要实现 MKAnnotation 协议。
3.给这个类定义一个类型为 CLLocationCoordinate2D 的属性,命名为 coordinate。特别要注意这个参数需要标示为只读类型的。因为 MKAnnotation 这个协议中定义的 Coordinate 也是只读类型的。

4.同理,定义两个 NSString 类型的属性,分别命名为 title 和 subtitle。这两个参数用来保存锚点的标题和内容信息。

5.给这个类添加一个初始化方法,这个方法需要接收一个 CLLocationCoordinate2D类型的参数。在这个方法中把我们在步骤 3 定义的那个属性传递进来..由于这个属性是只读的, 他并不能在这个类以外进行赋值。因此,这个初始化方法也就是一个桥梁,使我们能够正常的往这个了类中进行传递值。同理 title 和 subtitle 也要进行类似的操作。

6.初始化 MyAnnotation 这个类,然后把他添加到你的地图中,通过 Annotation 这个方法。 

如方案中所述,我们必须创建一个遵循 MKAnnotation 协议的对象,然后实例化这个对象,并将其传递给 map 以显示出来。 
.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>

//coordinate [kəʊ'ɔ:dɪneɪt] 坐标  annotation [ænə'teɪʃ(ə)n]注释
@property (nonatomic, readonly)CLLocationCoordinate2D coordinate;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *subtitle;

//初始化方法
- (id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subTitle:(NSString *)paramSubTitle;
@end

.m

#import "MyAnnotation.h"

@implementation MyAnnotation

- (id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subTitle:(NSString *)paramSubTitle{
    self = [super init];
    if (self != nil) {
        _coordinate = paramCoordinates;
        _title = paramTitle;
        _subtitle = paramSubTitle;
    }
    return self;
}
@end
然后实例化这个类,并将其添加到我们的map中。例如,在view controller的实现文件中,编写如下代码: 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myMapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
    self.myMapView.mapType = MKMapTypeStandard;
    self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;   
    self.myMapView.delegate = self;
    [self.view addSubview:_myMapView];

    //在地图上添加锚点
    //创建一个简单的位置 degrees 角度,度数 latitude纬度 longitude经度
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake(35.8219, 116.0225);
    // Create the annotation using the location
    MyAnnotation *annotation = [[MyAnnotation alloc]initWithCoordinates:location title:@"My Title" subTitle:@"My Sub Title"];
    //将锚点添加到地图上
    [_myMapView addAnnotation:annotation];
}

二 . 添加不同颜色的锚点

为了能够在地图视图中为我们的锚点自定义显示的颜色(从 SDK 中提供的锚点颜色中选 取),我们必须要获得一个 MKPinAnnotationView 这个实例对象而不是以往的 MKAnnotationView 对象。我们需要通过 mapView:viewForAnnotation 这个方法来获得这个实例对象。记住 MKPinAnnotationView 是 MKAnnotationView 的一个子类。具体实现方式请参考如下代码:
 
#pragma mark - MKMapViewDelegate  不同颜色的锚点
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    MKAnnotationView *result = nil;
    if ([annotation isKindOfClass:[MyAnnotation class]] == NO) {
        /* We want to process this event only for the Map View
         that we have created previously
         我们只为之前创建的地图视图处理这个事件*/
        return result;
    }
    
    MyAnnotation *senderAnnotation = (MyAnnotation *)annotation;
    /* Using the class method we have defined in our custom annotation class, we will attempt to get a reusable identifier for the pin we are about to create
     使用我们在自定义annotation类中定义的类方法,我们尝试获得一个将要创建的可重复利用的标识符指针*/
    NSString *pinReusableIdentifier = [MyAnnotation reusableIdentifierForPinColor:senderAnnotation.pinColor];
    /* Using the identifier we retrieved above, we will
     attempt to reuse a pin in the sender Map View
     使用上面检索到的标识符,我们将尝试重用该地图视图中的锚点指针*/
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinReusableIdentifier];
    if (annotationView == nil) {
        /* If we fail to reuse a pin, then we will create one 
         复用失败,创建一个新的锚点*/
        annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:senderAnnotation reuseIdentifier:pinReusableIdentifier];
        /* Make sure we can see the callouts on top of each pin in case we have assigned title and/or subtitle to each pin */
        [annotationView setCanShowCallout:YES];
    }
    
    annotationView.pinColor = senderAnnotation.pinColor;
    result = annotationView;
    return result;
}
我们应该在自定义的 MyAnnotation 类里针对每个锚点的唯一标识来设定回收机制,如 下是 MyAnnotation 类的头文件: 

 .h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
/* These are the standard SDK pin colors. We are setting
unique identifiers per color for each pin so that later we
can reuse the pins that have already been created with the same
color
 这些都是标准的SDK脚色。我们正在为每个引用制定每种颜色的唯一标识符,这样以后我们
 可以重用已经创建好具有相同的脚本颜色*/
#define REUSABLE_PIN_RED @"Red"
#define REUSABLE_PIN_GREEN @"Green"
#define REUSABLE_PIN_PURPLE @"Purple"
@interface MyAnnotation : NSObject<MKAnnotation>

/* unsafe_unretained since this is not an object. We can skip this and leave it to the compiler to decide. weak or strong won't work as this is not
 an object 
 unsafe_unretained,因为这不是一个对象。我们可以跳过这一点,并把它交给编译器来决定。强引用弱引用将无法正常工作,因为这是不一个对象*/
//coordinate [kəʊ'ɔ:dɪneɪt] 坐标  annotation [ænə'teɪʃ(ə)n]注释
@property (nonatomic, unsafe_unretained, readonly)CLLocationCoordinate2D coordinate;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *subtitle;
/* unsafe_unretained for the same reason as the coordinate property */
@property (nonatomic, unsafe_unretained)MKPinAnnotationColor pinColor;

//初始化方法
- (id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subTitle:(NSString *)paramSubTitle;

+ (NSString *)reusableIdentifierForPinColor:(MKPinAnnotationColor)paramColor;
@end

.m

#import "MyAnnotation.h"

@implementation MyAnnotation

- (id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subTitle:(NSString *)paramSubTitle{
    self = [super init];
    if (self != nil) {
        _coordinate = paramCoordinates;
        _title = paramTitle;
        _subtitle = paramSubTitle;
        _pinColor = MKPinAnnotationColorGreen;
    }
    return self;
}
+ (NSString *)reusableIdentifierForPinColor:(MKPinAnnotationColor)paramColor{
    NSString *result = nil;
    switch (paramColor) {
        case MKPinAnnotationColorRed:
            result = REUSABLE_PIN_RED;
            break;
        case MKPinAnnotationColorGreen:
            result = REUSABLE_PIN_GREEN;
            break;
        case MKPinAnnotationColorPurple:
            result = REUSABLE_PIN_PURPLE;
            break;
        default:
            break;
    }
    return result;
}
@end
//给锚点添加颜色
    annotation.pinColor = MKPinAnnotationColorPurple;
三 添加自定义的锚点~
MKMapView 实例对象的协议类必须要实现 MKMapViewDelegate 这个协议,并且要实现 mapView:viewForAnnotation 这个方法。这个方法将会返回一个 MKAnnotationView 的实例对象,任何这个类的子类都有一个叫做 image 的属性,我们可以通过给这个属性设置我们自定义的图片从而能够在地图上展示. 
 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //这个方法会自动调用,不重写的时候也自动调用,返回nil.
    MKAnnotationView *result = nil;
    if ([annotation isKindOfClass:[MyAnnotation class]] == NO) {
        
        return result;
    }
    
    MyAnnotation *senderAnnotation = (MyAnnotation *)annotation;
    
    NSString *pinReusableIdentifier = [MyAnnotation reusableIdentifierForPinColor:senderAnnotation.pinColor];
   
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinReusableIdentifier];
    if (annotationView == nil) {
      
        annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:senderAnnotation reuseIdentifier:pinReusableIdentifier];
       
        [annotationView setCanShowCallout:YES];
    }
    
    annotationView.pinColor = senderAnnotation.pinColor;
    
    //放上自定义图片
    UIImage *pinImage = [UIImage imageNamed:@"BluePin.png"];
    if (pinImage != nil) {
        annotationView.image = pinImage;
    }
    result = annotationView;
    return result;
}
 
 
 

 

原文地址:https://www.cnblogs.com/safiri/p/4081904.html