2015ztlucky

导航

MKMapView的使用

#import "ViewController.h"

#import "BVAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>

{

    CLLocationManager *locationManager;//声明管理者对象

    MKMapView *_mapView;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

//    初始化管理者对象

    locationManager = [[CLLocationManager alloc]init];

//    2.判断系统服务是否开启

    if (![CLLocationManager locationServicesEnabled]) {

        NSLog(@"系统服务未开启");

    }

//    3.判断授权状态

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {

        //如果是未授权状态时,则进行授权

//        授权是要修改plist文件

        if ([[[UIDevice currentDevice]systemVersion]floatValue]>= 8.0) {

            [locationManager requestWhenInUseAuthorization];

        }

        

    }

//    创建MKMapView

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

//    设置mapview的属性

//    设置mapView的跟踪模式

    _mapView.userTrackingMode = MKUserTrackingModeFollow;

//  设置mapView的类型

    _mapView.mapType =   MKMapTypeStandard;

//    设置代理

    _mapView.delegate = self;

    [self.view addSubview:_mapView];

    

#pragma-mark 添加button 用来控制回到定位的位置中心点

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, self.view.bounds.size.height-50-10, 80, 50)];

    btn.backgroundColor = [UIColor cyanColor];

    [btn addTarget:self action:@selector(btnAction:) forControlEvents:

     UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

#pragma -mark 通过点击手势添加大头针

    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(follow:)];

    [_mapView addGestureRecognizer:tap];

    

}

#pragma -mark 通过点击手势添加大头针的点击方法

-(void)follow:(UITapGestureRecognizer *)tap{

    CGPoint point = [tap locationInView:_mapView];

//    把坐标点转换成地理坐标

   CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];

//   在该地理坐标位置创建大头针

    BVAnnotation *annotation = [[BVAnnotation alloc]init];

    annotation.coordinate = coordinate;

    annotation.title = @"中华大地";

    annotation.subtitle  = @"欢迎你";

    annotation.image = [UIImage imageNamed:@"category_4"];

    [_mapView addAnnotation:annotation];

}

#pragma -mark 自定义大头针的样式

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

//    判断这个大头针是不是系统定位的大头针 如果是,则不用重新定义

//    方法的使用类似于UiTableViewCell的创建和复用

    if ([annotation isKindOfClass: [BVAnnotation class]])

    {

        

        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"annotationView"];

        if (annotationView == nil) {

            annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"annotationView"];

//            设置选中时显示详情框

            annotationView.canShowCallout = YES;

            

//            添加辅助视图(左右两边)

            annotationView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

            

//            annotationView.image = [UIImage imageNamed:@""];

           

        }

        annotationView.annotation = annotation;

         annotationView.image = ((BVAnnotation *)annotation).image;

 

        return annotationView;

        

    }

    

    return nil;

}

 

#pragma -mark 回到定位的位置的but事件响应的方法

-(void)btnAction:(UIButton *)button{

    

    [ _mapView setCenterCoordinate:_mapView.userLocation.location.coordinate animated:YES];

    

}

#pragma -mark 代理方法

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

//    完成位置更新的代理方法

    NSLog(@"完成更新");

    

//    设置地图显示区域

    MKCoordinateSpan span = MKCoordinateSpanMake(1, 1);

    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, span);

    [mapView setRegion:region];

    

}

-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{

//   地图完成加载

    NSLog(@"完成加载");

}

 ========BVAnnotation==========

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface BVAnnotation : NSObject<MKAnnotation>

//重新定义协议里的属性

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

 

 

@property (nonatomic, copy) NSString *title;

@property (nonatomic,copy) NSString *subtitle;

@property(nonatomic,retain)UIImage *image;

@end

 

posted on 2016-02-23 14:30  2015ztlucky  阅读(243)  评论(0编辑  收藏  举报