添加自定义大头针

 

1)创建一个继承于NSOject的大头针对象类

2)在MKAnnotation类中导入MapKit,添加协议<MKAnnotation>

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject<MKAnnotation>

3)按住command的键到<MKAnnotation>中查找要添加的属性,并删除只读readonly

 

添加大头针属性

@property (nonatomic) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

 可以创建对象属性,给大头针添加信息

@property (nonatomic, strong)Business *business;

4)在地图类中引入大头针类,创建大头针对象并给其属性赋值

 添加在地图中

#import "MyAnnotation.h"

 

    //从网络中提取信息

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];

    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    

    [dic setObject:[ud objectForKey:@"city"] forKey:@"city"];

    

    NSArray *bussinesses = [DianpingApi requestBusinessesWithParams:dic];

    //创建大头针,并将数组中的信息分别赋给大头针

    for (Business *b in bussinesses) {

        //排除掉不符合条件的信息(这里是位置信息不符合)

        if (b.coord.latitude>200) {

            continue;

        }

        //为每个信息创建相应的大头针,并将信息存储在大头针内

        MyAnnotation *ann = [[MyAnnotation alloc]init];

        ann.coordinate = b.coord;

        ann.business = b;

        //添加大头针到地图中

        [self.mapView addAnnotation:ann];

    

        //设置mapView的显示位置与缩放比例

        [self.mapView setRegion:MKCoordinateRegionMake(b.coord, MKCoordinateSpanMake(.1, .1))];

        

    }

5在MapViewController类中引入#import "MyAnnotationView.h"并添加<MKMapViewDelegate>协议并在storyboard中给mapView连线,添加协议

 

#import "MyAnnotationView.h"

@interface ViewController ()<MKMapViewDelegate>

 

 

 6)按住Command键点击<MKMapViewDelegate>协议,寻找方法

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

 

 

 显示在MapView中显示大头针(注释)

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

    //如果Map页面中有大头针视图就复用原视图

    MyAnnotationView *av = (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ann"];

    

    if (!av) {

        

        //如果MapView中没有大头针就通过自定义MKAnnotationView创建大头针图像

        av = [[MyAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ann"];

    }

    

    return av;

}

 

 

 //给打头针添加点击事件

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

    

    MyAnnotation *ann = view.annotation;

    

    Business *b = ann.business;

    

    

    UIViewController *vc = [[UIViewController alloc]init];

    UIWebView *wv = [[UIWebView alloc]initWithFrame:vc.view.bounds];

    NSURL *url = [NSURL URLWithString:b.path];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    [wv loadRequest:request];

    

    

    [vc.view addSubview:wv];

    

    [self.navigationController pushViewController:vc animated:YES];

 

    

    

}

 

 

7)在自定义MKAnnotationView中自定义Annotation的图像

打initWithFrame

将其修改为

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    if (self) {

        <#statements#>

    }

    return self;

}

在其中修改大头针的图片、添加其他信息

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    if (self) {

        

        //修改大头针的样式

//        self.p_w_picpath = [UIImage p_w_picpathNamed:@"index_map.png"];

        

     

        //通过大头针提取信息,并将信息显示出来,

        MyAnnotation *ann = annotation;

        

        Business *b = ann.business;

        

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:b.img]];

        self.p_w_picpath = [UIImage p_w_picpathWithData:data];

        //设置大头针View的尺寸

        self.bounds = CGRectMake(0, 0, 50, 50);

    }

    return self;

}