iOS定位添加大头针

地图结合的定位
1. 和单纯定位相同部分
iOS8+都必须征求用户的同意
2. 和单纯定位不同部分
a. 协议:MKMapViewDelegate (MapKit Framework)
b.类/控件:地图控件MKMapView
c. 定位的方式也不同: 通过地图视图的一个属性
细节:
a. 如果手动往storyboard中拖拽一个MKMapView, 需要手动导入MapKit Framework;
b. 如果用纯代码的方式添加MKMapView, a步骤省略
c. CLLocationManager对象声明一个属性,不会造成弹出框很快收回 (!!!!!)

viewController.m

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

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

//CLLocationManager
@property (nonatomic,strong) CLLocationManager *mgr;

//CLGeocoder
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mgr = [CLLocationManager new];
    self.geocoder = [CLGeocoder new];

    //征求用户的意见 (Info.plist)
    [self.mgr requestWhenInUseAuthorization];

    //设置代理
    self.mapView.delegate = self;

    //设置地图的属性
    self.mapView.rotateEnabled = YES;

    //设置地图的显示方式(卫星/卫星混合)
    self.mapView.mapType = MKMapTypeStandard;

    //执行定位操作
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
}

#pragma mark --- MapView Delegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //使用反地理编码来获取用户位置对应的详细信息(国家/省)
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //使用placemark给定的信息,设置用户位置大头针的文本信息
        CLPlacemark *placemark = [placemarks lastObject];
        //title
        userLocation.title = placemark.name;

        //subtitle
        userLocation.subtitle = placemark.locality;
    }];

    //显示地图
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}


//地图拖拽的情况调用方法
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    NSLog(@"span: %f; %f", self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
}

//自定义大头针必须实现的代理方法
//1.地图上有多少大头针,调用多少次
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSLog(@"自定义大头针方法");
    //6.如果默认返回nil,当前的用户位置为默认的蓝色;其他的默认是红色的
// return nil;
    //把用户位置所属的大头针类型排除在外
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        //当前的大头针对象annotation是用户的位置
        return nil;//蓝色
    }

    //2.使用重用机制
    //2.1 声明一个可重用的大头针identifier
    static NSString *identifier = @"Annotation";
    //2.2 从缓冲池中获取可重用的大头针对象,如果有,就直接放回
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    //2.3 如果缓冲池中没有,再重现创建大头针对象
    if (!pinAnnotation) {
        //3.第一个参数必须nil(?????)
        pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
        //4.必须要设置一个显示弹出框的属性(否则title/subtitle无法显示)
        pinAnnotation.canShowCallout = YES;

        //设置大头针左右辅助视图
        pinAnnotation.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_classify_cafe"]];
        pinAnnotation.rightCalloutAccessoryView = [[UISwitch alloc] init];
    }

    //设置pin大头针的属性(iOS9会多一个自定义颜色的属性)
    pinAnnotation.pinColor = MKPinAnnotationColorPurple;
    //7.大头针视图的动画和图片设置不能同时
// pinAnnotation.animatesDrop = YES;
    //设置一个大头针的图片
    pinAnnotation.image = [UIImage imageNamed:@"icon_classify_cafe"];

    //5.将代理方法中的形参annotation赋值给pinAnnoation视图对象
    pinAnnotation.annotation = annotation;

    return pinAnnotation;
}

//手动添加两个大头针
- (IBAction)addAnnotation:(id)sender {

    //创建两个自定义的大头针对象
    TRAnnotation *firstAnnotation = [[TRAnnotation alloc] init];
    TRAnnotation *secondAnnotation = [[TRAnnotation alloc] init];

    //设置添加大头针的属性(位置/title文本/subtitle子文本)
    firstAnnotation.coordinate = CLLocationCoordinate2DMake(39.875, 116.456);
    firstAnnotation.title = @"iOS1506";
    firstAnnotation.subtitle = @"Fighting";

    secondAnnotation.coordinate = CLLocationCoordinate2DMake(39.879, 116.451);
    secondAnnotation.title = @"iOS1506";
    secondAnnotation.subtitle = @"AZAZA";

    //添加到地图上
    [self.mapView addAnnotation:firstAnnotation];
    [self.mapView addAnnotation:secondAnnotation];

    //设置地图显示的区域(中心/跨度)
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
    MKCoordinateRegion region = MKCoordinateRegionMake(secondAnnotation.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface TRAnnotation : NSObject <MKAnnotation>

//位置(required必须)
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

//title(optional可选)
@property (nonatomic, copy) NSString *title;

//subtitle(optional可选)
@property (nonatomic, copy) NSString *subtitle;

@end

添加自定义大头针的步骤(方式一)
1. 创建一个自定义的类(a.b.c)
2. 实现代理的方法
- (MKAnnotationView )mapView:(MKMapView )mapView viewForAnnotation:(id)annotation {}
3. 在第二步的代理方法要执行操作
a. 首先把用户大头针对应的类要排除
b. 其次要手动创建大头针的重用机制(和table view cell类似)
c. 从缓存池中获取的大头针视图转换类型MKPinAnnotationView
d. 设定大头针的属性(颜色、动画/图片)

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

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

//CLLocationManager
@property (nonatomic,strong) CLLocationManager *mgr;

//CLGeocoder
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mgr = [CLLocationManager new];
    self.geocoder = [CLGeocoder new];

    //征求用户的意见 (Info.plist)
    [self.mgr requestWhenInUseAuthorization];

    //设置代理
    self.mapView.delegate = self;

    //设置地图的属性
    self.mapView.rotateEnabled = YES;

    //设置地图的显示方式(卫星/卫星混合)
    self.mapView.mapType = MKMapTypeStandard;

    //执行定位操作
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
}

#pragma mark --- MapView Delegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //使用反地理编码来获取用户位置对应的详细信息(国家/省)
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //使用placemark给定的信息,设置用户位置大头针的文本信息
        CLPlacemark *placemark = [placemarks lastObject];
        //title
        userLocation.title = placemark.name;

        //subtitle
        userLocation.subtitle = placemark.locality;
    }];

    //显示地图
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}


//地图拖拽的情况调用方法
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    NSLog(@"span: %f; %f", self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
}

//自定义大头针必须实现的代理方法
//1.地图上有多少大头针,调用多少次
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSLog(@"自定义大头针方法");
    //6.如果默认返回nil,当前的用户位置为默认的蓝色;其他的默认是红色的
// return nil;
    //把用户位置所属的大头针类型排除在外
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        //当前的大头针对象annotation是用户的位置
        return nil;//蓝色
    }

    //2.使用重用机制
    //2.1 声明一个可重用的大头针identifier
    static NSString *identifier = @"Annotation";
    //2.2 从缓冲池中获取可重用的大头针对象,如果有,就直接放回
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    //2.3 如果缓冲池中没有,再重现创建大头针对象
    if (!pinAnnotation) {
        //3.第一个参数必须nil(?????)
        pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
        //4.必须要设置一个显示弹出框的属性(否则title/subtitle无法显示)
        pinAnnotation.canShowCallout = YES;

        //设置大头针左右辅助视图
        pinAnnotation.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_classify_cafe"]];
        pinAnnotation.rightCalloutAccessoryView = [[UISwitch alloc] init];
    }

    //设置pin大头针的属性(iOS9会多一个自定义颜色的属性)
    pinAnnotation.pinColor = MKPinAnnotationColorPurple;
    //7.大头针视图的动画和图片设置不能同时
// pinAnnotation.animatesDrop = YES;
    //设置一个大头针的图片
    pinAnnotation.image = [UIImage imageNamed:@"icon_classify_cafe"];

    //5.将代理方法中的形参annotation赋值给pinAnnoation视图对象
    pinAnnotation.annotation = annotation;

    return pinAnnotation;
}

//手动添加两个大头针
- (IBAction)addAnnotation:(id)sender {

    //创建两个自定义的大头针对象
    TRAnnotation *firstAnnotation = [[TRAnnotation alloc] init];
    TRAnnotation *secondAnnotation = [[TRAnnotation alloc] init];

    //设置添加大头针的属性(位置/title文本/subtitle子文本)
    firstAnnotation.coordinate = CLLocationCoordinate2DMake(39.875, 116.456);
    firstAnnotation.title = @"iOS1506";
    firstAnnotation.subtitle = @"Fighting";

    secondAnnotation.coordinate = CLLocationCoordinate2DMake(39.879, 116.451);
    secondAnnotation.title = @"iOS1506";
    secondAnnotation.subtitle = @"AZAZA";

    //添加到地图上
    [self.mapView addAnnotation:firstAnnotation];
    [self.mapView addAnnotation:secondAnnotation];

    //设置地图显示的区域(中心/跨度)
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
    MKCoordinateRegion region = MKCoordinateRegionMake(secondAnnotation.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}

创建自定义大头针的方式二
1. 使用MKAnnotationView
2. 创建自定义大头针对象不同(添加一个图片属性)
3. 代理的实现逻辑不同

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

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

//CLLocationManager
@property (nonatomic,strong) CLLocationManager *mgr;

//CLGeocoder
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mgr = [CLLocationManager new];
    self.geocoder = [CLGeocoder new];

    //征求用户的意见 (Info.plist)
    [self.mgr requestWhenInUseAuthorization];

    //设置代理
    self.mapView.delegate = self;

    //设置地图的属性
    self.mapView.rotateEnabled = YES;
    //设置地图的显示方式(卫星/卫星混合)

    //执行定位操作
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
}



#pragma mark --- MapView Delegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //使用反地理编码来获取用户位置对应的详细信息(国家/省)
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //使用placemark给定的信息,设置用户位置大头针的文本信息
        CLPlacemark *placemark = [placemarks lastObject];
        //title
        userLocation.title = placemark.name;

        //subtitle
        userLocation.subtitle = placemark.locality;
    }];

    //显示地图
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    //把用户的位置对应的大头针排除
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;//蓝色圈
    }

    //重用机制
    static NSString *identifier = @"Annotation";
    //不用转换
    MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
        //显示title/subtitile属性
        annotationView.canShowCallout = YES;
    }

    //方式二(MKAnnotionaView)和方式一(MKPinAnnotationView)的不同
    TRAnnotaion *anno = (TRAnnotaion *)annotation;
    annotationView.annotation = anno;
    annotationView.image = anno.image;

    return annotationView;
}

- (IBAction)addAnnotation:(id)sender {

    //创建两个大头针对象
    TRAnnotaion *firstAnnoation = [[TRAnnotaion alloc] init];
    TRAnnotaion *secondAnnotion = [[TRAnnotaion alloc] init];
    firstAnnoation.coordinate = CLLocationCoordinate2DMake(39.875, 116.342);
    firstAnnoation.title = @"iOS1506";
    firstAnnoation.subtitle = @"Fighting";
    //不同
    firstAnnoation.image = [UIImage imageNamed:@"icon_paopao_waterdrop_streetscape"];

    secondAnnotion.coordinate = CLLocationCoordinate2DMake(39.877, 116.457);
    secondAnnotion.title = @"iOS1506";
    secondAnnotion.subtitle = @"AZAZA";
    //不同
    secondAnnotion.image = [UIImage imageNamed:@"icon_pin_floating"];

    //添加到地图上; 设定地图的区域(中心/跨度)
    [self.mapView addAnnotation:firstAnnoation];
    [self.mapView addAnnotation:secondAnnotion];

    MKCoordinateSpan span = MKCoordinateSpanMake(0.2, 0.2);
    MKCoordinateRegion region = MKCoordinateRegionMake(firstAnnoation.coordinate, span);
    [self.mapView setRegion:region animated:YES];   
}

你可能感兴趣的:(iOS定位添加大头针)