获取当前位置
- Corelocation获取位置满足一下2个要求:
- 得到用户的许可
- 确保设备启用定位服务
使用
- 1 . 倒入框架 MapKit Corelocatin
- 2 . 倒入Corelocation Mapkit 头文件
- 3 . 向用户发出请求
- 1 . 在infoplist 中添加key NSLocationWhenInUseUsageDescription 或NSLocationAlwaysUsageDescription申请时给用户提示的文字作为key的内容。
- 2 . 在代码中申请授权
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestAlwaysAuthorization];//总是
//[self.locationManager requestWhenInUseAuthorization];
}
- 3 .用户许可和响应
//申请授权,如果用户没有选择过
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
//当应用在前台时授权
// [self.locationManager requestAlwaysAuthorization];//总是
[self.locationManager requestWhenInUseAuthorization];
}
- 4 .一些配置
```
//更新频率
self.locationManager.distanceFilter = 10.0f;
//精度
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//定位服务是否可用
if ([CLLocationManager locationServicesEnabled]) {
[self.locationManager startUpdatingLocation];//开始定位
}else{
//暂停定位
}
//设置mapView 的delegate
self.mapVIew.delegate = self;
//修改地图的类型
//[self.mapVIew setMapType:MKMapTypeHybrid];
- 5 . 授权改变的代理方法
//授权状态的改变
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
NSLog(@"%d",status);
}
- 6 . 定位到位置时的代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"locations%@",locations);
CLLocation *location = locations.lastObject;
//如果水平精度过差,就放弃该店
if (location.horizontalAccuracy>kCLLocationAccuracyHundredMeters) {
return;
}
CLLocationCoordinate2D coordinate = location.coordinate;
//把地图调整到定位区域
//定位到的点作为中心点
//设定一个跨度
MKCoordinateSpan pan = MKCoordinateSpanMake(0.05, 0.05);
//构造区域
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, pan);
//将地图调整到该区域
[self.mapVIew setRegion:region animated:YES];
// //添加一个系统标注点
// MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
// annotation.coordinate = coordinate;
// [self.mapVIew addAnnotation:annotation];
//添加一个自定义的标注点
QYAnnotation *annotation = [[QYAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"香港";
annotation.subtittle = @"就在这";
[self.mapVIew addAnnotation:annotation];
}
- 自定义的 MKAnnotation 类 QYAnonotation
// Copyright © 2016 qingyun. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface QYAnnotation : NSObject<MKAnnotation>
//位置点方法
@property (nonatomic ) CLLocationCoordinate2D coordinate;
//标题和子标题
@property (nonatomic, copy) NSString *title; //重写MkAnnotation的属性 不能写错
@property (nonatomic, copy) NSString *subtittle;
@end
- 7 .发生错误时调用的代理方法
//发生错误的时候
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%@",error);
}
- 8 .返回注释视图的代理方法
//返回注释的delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
if ([annotation isKindOfClass:[QYAnnotation class]]) {//我门自己添加的
//定义一个复用的字符串
static NSString *indentifier = @"qingyun";
//调用复用的方法
MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:indentifier];
if (!view) {
view = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:indentifier];
}
//绑定数据
view.annotation = annotation;
view.image = [UIImage imageNamed:@"iconpng"];
view.centerOffset = CGPointMake(0, -15);
view.canShowCallout = YES;//是否显示
return view;
}
return nil;
}
Mapkit 的代理
#pragma --mark mapViewdelegate
//区域改变的时候
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"区域改变");
}
//将地图诠释推到详细的视图控制器时,告诉委托用户利用注释视图的一个辅助按钮。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
NSLog(@"%@",view);
}