IOS中的地图视图MKMapView

来源:互联网 发布:二级c语言上机题库 编辑:程序博客网 时间:2024/05/22 16:53

.m文件

#import "ViewController.h"

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


@interfaceViewController () <MKMapViewDelegate,CLLocationManagerDelegate>

{

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

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


}


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


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    [selfinitUserInterface];

}


- (void)initUserInterface

{

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

    

    _locationManager = [[CLLocationManageralloc] init];

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

    [_locationManagerstartUpdatingLocation];

    _locationManager.delegate =self;

    

    /*     

    想要弹出提示框:

    必须在plist中添加key -- NSLocationWhenInUseUsageDescription

     */



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

//   添加key---NSLocationAlwaysUsageDescription

    

    

    _mapView = [[MKMapViewalloc] initWithFrame:self.view.bounds];

    

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

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


    _mapView.delegate =self;

    

    [self.viewaddSubview:_mapView];

    

   //添加长按手势识别器

    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(pressedLongPress:)];

    [_mapViewaddGestureRecognizer: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 = [[MKPointAnnotationalloc] init];

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

        CGPoint point = [senderlocationInView:_mapView];

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

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

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

        annotation.coordinate = coordinate;

         //4.配置标题

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

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

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

        [_mapViewaddAnnotation: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 ([annotationisKindOfClass:[MKUserLocationclass]]) {

        returnnil;

    }

   //标注视图的重用

    staticNSString * annotationViewID = @"chongyong";

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

    if (!annotationView) {

        annotationView = [[MKPinAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:annotationViewID];

    }

    

   //配置大头针颜色

    annotationView.pinColor =MKPinAnnotationColorPurple;

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

    annotationView.animatesDrop =YES;

    

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

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

    

   //配置弹出信息框

    UIButton * startButton = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

    startButton.layer.cornerRadius =3;

    startButton.backgroundColor = [UIColorgrayColor];

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

    annotationView.rightCalloutAccessoryView = startButton;

    

    return annotationView;

}


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

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

{

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

    CLLocation * currentLocation = [[CLLocationalloc] initWithLatitude:coordiante.latitudelongitude:coordiante.longitude];

    

    //地理位置逆编码

    CLGeocoder * geoCoder = [[CLGeocoderalloc] 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

{


    [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"http://www.baidu.com"]];


}





/*****  总结

 

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

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

 

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

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

 

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

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

 


 *****/




@end


0 0
原创粉丝点击