| 关键词: nbsp 大头针 mapView annotation annotationView 定位服务 视图 CLLocationManage 位置 定位 |
使用定位服务: 设置app有访问定位服务的权限,首先要在info.plist文件中添加以下liang'ti 1.NSLocationWhenInUseUsageDescription 在使用应用期间 2.NSLocationAlwaysUsageDescription 始终 #import "ViewController.h" #import <CoreLocation/CoreLocation.h> //定位服务 #import <MapKit/MapKit.h> //地图界面 #import "WXAnnotation.h" @interface ViewController ()<MKMapViewDelegate> { CLLocationManager *_manager; MKMapView *_mapView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //接下来创建一个对象来管理相关的定位服务 _manager = [[CLLocationManager alloc] init]; //manager判断: 手机是否开启定位 / app是否有访问定位的权限 //[CLLocationManager locationServicesEnabled]; //手机是否开启定位 //[CLLocationManager authorizationStatus]; //app访问定位的权限的状态 if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse ){ [_manager requestWhenInUseAuthorization]; //向用户请求访问定位服务 } //创建地图 [self createMapView]; //创建大头针 [self createAnnotationView]; } - (void)createMapView { //创建MapView _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; //地图的类型 _mapView.mapType = MKMapTypeStandard; //显示用户当前的位置 _mapView.showsUserLocation = YES; //是否定位(是否追终用户的位置) _mapView.userTrackingMode = MKUserTrackingModeFollow; _mapView.delegate = self; [self.view addSubview:_mapView]; } //创建大头针 - (void)createAnnotationView { //创建model对象 WXAnnotation *annotation = [[WXAnnotation alloc] init]; annotation.title = @"定位的位置"; annotation.subtitle = @"杀杀杀!"; //坐标的结构体 CLLocationCoordinate2D coord = { 39.9927359964, 116.3965684639 }; //大头针所在的坐标位置 annotation.coordinate = coord; //把标注添加给mapView [_mapView addAnnotation:annotation]; } //更新用户的位置(地图视图的代理方法) - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { //根据经纬度设置MapView //设置显示的区域范围,设置显示的精度 //设置显示的精度(比列尺) MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01); //指定的范围 MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate,span); //移动mapView显示的位置 [_mapView setRegion:region animated:YES]; } //创建标注视图的代理方法 /** * 调用时机:annotation要添加到mapView上,坐标出现在mapView中,就调用代理方法添加一个大头针 */ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { //当前用户的位置,不显示大头针 if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } // MKAnnotationView 不能直接使用 (自定义大头针视图,继承) //MKPinAnnotationView 能显示的大头针视图 static NSString *iden = @"Annotation_view"; //大头针视图 MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:iden]; if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:iden]; //大头针的颜色 annotationView.pinColor = MKPinAnnotationColorPurple; //大头针从天而降的动画 annotationView.animatesDrop = YES; //显示内容 annotationView.canShowCallout = YES; //辅助视图 annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; } //重新设置大头针视图的model,解决重用的问题 annotationView.annotation = annotation; return annotationView; } |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|