地图与位置服务笔记

//第一步:创建标注Annotation类,实现MKAnnotation协议

#import "ViewController.h"

#import "AnnotationModel.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    manager = [[CLLocationManager alloc] init];

    

    [manager requestAlwaysAuthorization];

    

    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

    mapView.delegate = self;

    //设置地图类型

    mapView.mapType = MKMapTypeStandard;

    //设置当前用户的位置

    mapView.showsUserLocation = YES;

    

    //设置经纬度

    CLLocationCoordinate2D center = {39.9145402256,116.1767592387};

    

    //设置精确度,数值越大,显示的范围越大

    MKCoordinateSpan span = {0.1,0.1};

    

    //创建一个区域

    MKCoordinateRegion region = {center,span};

    

    //设置显示的区域

    [mapView setRegion:region animated:YES];

    

    [self.view addSubview:mapView];

    

    //第二步:创建标注Annotation对象

    CLLocationCoordinate2D coord1 = {39.9117669028,116.1933922217};

    AnnotationModel *model1 = [[AnnotationModel alloc] initWithCoordinate:coord1];

    model1.title = @"石景山公园";

    model1.subtitle = @"北京石景山公园";

    

    CLLocationCoordinate2D coord2 = {39.9261543081,116.1776545774};

    AnnotationModel *model2 = [[AnnotationModel alloc] initWithCoordinate:coord2];

    model2.title = @"苹果园";

    model2.subtitle = @"石景山区苹果园";

    

    CLLocationCoordinate2D coord3 = {39.8637359235,116.2854074940};

    AnnotationModel *model3 = [[AnnotationModel alloc] initWithCoordinate:coord3];

    model3.title = @"丰台公园";

    model3.subtitle = @"丰台区丰台公园";

    

    CLLocationCoordinate2D coord4 = {39.9128821069,116.3971393161};

    AnnotationModel *model4 = [[AnnotationModel alloc] initWithCoordinate:coord4];

    model4.title = @"故宫博物馆";

    model4.subtitle = @"北京市故宫博物馆";

    

    //第三步:将Annotation对象添加到MapView

    [mapView addAnnotation:model1];

    [mapView addAnnotation:model2];

    [mapView addAnnotation:model3];

    [mapView addAnnotation:model4];


    

}

#pragma mark -MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    NSLog(@"userLocation:%@",userLocation);

}

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

{

    NSLog(@"view is %@",view);

}

//第四步:最后实现MKMapViewDelegate协议方法,在该代理中创建MKPinAnnotationView标注视图

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

{

    NSLog(@"annotation:%@",annotation);

    

    //自己的位置也是一个标注视图,它是一个属于MKUserLocation这么一个类

    if ([annotation isKindOfClass:[MKUserLocation class]]) {

        //如果满足条件,说明当前用户自己的位置,不需要我们设置标注视图

        return nil;

    }

    static NSString *identifier = @"annotationView";

    //MKPinAnnotationView是一个大头针视图

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView == nil) {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];

        //设置大头针的颜色

        annotationView.pinColor = MKPinAnnotationColorPurple;

        //设置显示从天而降的动画

        annotationView.animatesDrop = YES;

        //是否显示标题视图

        annotationView.canShowCallout = YES;

        //设置辅助视图

        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    }

    //防止复用

    annotationView.annotation = annotation;

    return annotationView;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


你可能感兴趣的:(我的文章)