IOS中的地图视图MKMapView

.m文件

#import "ViewController.h"

#import <MapKit/MapKit.h>  //导入框架


@interface ViewController () <MKMapViewDelegate,CLLocationManagerDelegate>

{

    MKMapView * _mapView;                 //地图,集成了定位功能

    CLLocationManager * _locationManager; //位置管理者,可以只使用来定位


}


- (void)initUserInterface;  //初始化用户界面


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    [self initUserInterface];

}


- (void)initUserInterface

{

//    [CLLocationManager locationServicesEnabled];  //检测设备是否支持定位

    

    _locationManager = [[CLLocationManager alloc] init];

    [_locationManager requestWhenInUseAuthorization]; //请求授权(弹出警告询问是否允许应用程序获取当前位置信息)

    [_locationManager startUpdatingLocation];

    _locationManager.delegate = self;

    

    /*     

     想要弹出提示框:

     必须在plist中添加key -- NSLocationWhenInUseUsageDescription

     */



//    [_locationManager requestAlwaysAuthorization];//请求后台运行时获取位置授权

//    添加key---NSLocationAlwaysUsageDescription

    

    

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

    

    _mapView.showsUserLocation = YES;  //显示用户位置

//    _mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;//用户跟随模式(把地图缩放到定位地点的适合大小)


    _mapView.delegate = self;

    

    [self.view addSubview:_mapView];

    

    //添加长按手势识别器

    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressedLongPress:)];

    [_mapView addGestureRecognizer:longPress];


}


#pragma mark - <CLLocationManagerDelegate>

//更新了用户位置

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{


    CLLocation * lastLocation = locations.lastObject;


}



#pragma mark - Pressed

- (void)pressedLongPress:(UIGestureRecognizer *)sender

{

    //在长按手势开始的时候添加地图标注(系统地图的用户体验)

    if (sender.state == UIGestureRecognizerStateBegan) {

        

        //.构建标注数据源

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

          //1.获取视图上的长按点

        CGPoint point = [sender locationInView:_mapView];

          //2.将视图上的点转换为经纬度坐标

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

          //3.配置标注数据源的坐标

        annotation.coordinate = coordinate;

          //4.配置标题

        annotation.title = @"坐标获取中..";

//        annotation.subtitle = @"call me";

        //.添加这个坐标的标注到地图上

        [_mapView addAnnotation:annotation];

    }

}


#pragma mark - <MKMapViewDelegate>

//更新用户位置后

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

{


//MKUserLocation -> location(CLLocation) -> coordinate(CLCoordinate2D)

//    userLocation.location.coordinate.longitude//经度

//    userLocation.location.coordinate.latitude//纬度

    

    //设置当前地图显示的区域和范围

    [mapView setRegion:MKCoordinateRegionMake(userLocation.location.coordinate, MKCoordinateSpanMake(0.01, 0.01)) animated:YES];


}


//自定义标注视图(只要是添加到地图上的标注都会通过这个方法进行标注视图配置)

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

{

    //MKUserLocationMKPointAnnotatin都遵循标注协议,可以作为地图标注

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

        return nil;

    }

    //标注视图的重用

    static NSString * annotationViewID = @"chongyong";

    MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];//MKPinAnnotationViewMKAnnotationView的子类   要使用图片配置大头针时使用MKAnnotationView

    if (!annotationView) {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewID];

    }

    

    //配置大头针颜色

    annotationView.pinColor = MKPinAnnotationColorPurple;

    //配置大头针的掉落效果

    annotationView.animatesDrop = YES;

    

//    annotationView.image = [UIImage imageNamed:@"iconfont-dituzuobiao"];  //MKAnnotationView时可以配置

    annotationView.canShowCallout = YES; //是否允许弹框

    

    //配置弹出信息框

    UIButton * startButton = [UIButton buttonWithType:UIButtonTypeSystem];

    startButton.frame = CGRectMake(0,0,60,25);

    startButton.layer.cornerRadius = 3;

    startButton.backgroundColor = [UIColor grayColor];

    [startButton setTitle:@"到这里" forState:UIControlStateNormal];

    annotationView.rightCalloutAccessoryView = startButton;

    

    return annotationView;

}


//当选中了某个标注视图的时候

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

{

    CLLocationCoordinate2D coordiante = ((MKPointAnnotation *)view.annotation).coordinate;

    CLLocation * currentLocation = [[CLLocation alloc] initWithLatitude:coordiante.latitude longitude:coordiante.longitude];

    

    //地理位置逆编码

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];

    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        

        if (placemarks.count > 0) {

            CLPlacemark * placemark = placemarks[0];

            ((MKPointAnnotation *)view.annotation).title = placemark.thoroughfare;

            ((MKPointAnnotation *)view.annotation).subtitle = placemark.subThoroughfare;

        }

        if (error) {

            ((MKPointAnnotation *)view.annotation).title = @"获取失败";

        }

//        NSLog(@"%@",placemarks[0]);

        

    }];

}


//点击信息框中的按钮

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

{


    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];


}





/*****   总结

 

 //CLLocation 包含了坐标、水平高度等的位置

 //CLLoactionCoorDinate2D 包含经纬度信息,是一个坐标

 

 //MKPointAnnotation  标注,包含坐标及标题、子标题  <MKAnnotation>

 //MKUserLocatin  用户位置标注,包含坐标、标题、子标题  <MKAnnotation>

 

 //MKAnnotationView 标注视图,包含标注  <MKAnnotation>

 //MKPinAnnotationView 大头针样式的标注视图

 


 *****/




@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值