iOS 获取定位的方法有多种,集成三方地图sdk,利用系统定位获取都是可以的,这里就描述一下利用系统获取定位的方法
1.首先要做的就是在info中做配置
我一般是三个配置方式都做上,也可根据需要做相应配置
Privacy - Location Usage Description
您的位置信息将使用于区域用户统计,使用后台位置功能可能会增加电量消耗
Privacy - Location Always Usage Description
您的位置信息将使用于区域用户统计,使用后台位置功能可能会增加电量消耗
Privacy - Location When In Use Usage Description
您的位置信息将使用于区域用户统计,使用后台位置功能可能会增加电量消耗
注:这里的后面汉字,以前苹果只要求做免责,现在需要告诉用户获取定位用途。所以防止提版被拒就使用:用途+免责
2.引头文件
#import <MapKit/MapKit.h>
3.遵守协议,声明属性
CLLocationManagerDelegate
@property (strong, nonatomic) CLLocationManager* locationManager;
4.代码实现定位
//开始定位
-(void)startLocation{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
if ([[[UIDevice currentDevice]systemVersion]doubleValue] >8.0){
[self.locationManager requestWhenInUseAuthorization];
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
_locationManager.allowsBackgroundLocationUpdates =YES;
}
[self.locationManager startUpdatingLocation];
}
//这个方法用来获取用户是否开启可定位权限
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
casekCLAuthorizationStatusNotDetermined:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
break;
default:
break;
}
}
//获得的定位
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *newLocation = locations[0];
CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
[manager stopUpdatingLocation];
//oldCoordinate.longitude 经度
//oldCoordinate.latitude 纬度
}
注:只需要调用上面的startLocation方法就好