IOS前台定位:
1. 获取LocationManager
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationM;
@end
@implementation ViewController
/** 位置管理者 */
- (CLLocationManager *)locationM
{
if (!_locationM) {
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;
// IOS8+
if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
// 如果是iOS8,需要请求授权方式(进行判断,否则在iOS7会崩溃,需要先在info.plist中配置)
// ios 7执行该方法会奔溃
// 和Privacy - Location Always and When In Use Usage Description对应,允许前后台定位
// [_locationM requestAlwaysAuthorization];
// 和Privacy - Location When In Use Usage Description 对应 允许前台定位
[_locationM requestWhenInUseAuthorization];
// 后台定位
// if([[UIDevice currentDevice].systemVersion floatValue] >= 9.0){
// _locationM.allowsBackgroundLocationUpdates = YES;
// }
}
_locationM.desiredAccuracy = kCLLocationAccuracyBest;
}
return _locationM;
}
2. 设置权限回调:
/**
* 当前定位授权状态发生改变时调用
*
* @param manager 位置管理者
* @param status 状态, 申请权限状态返回有代理
*/
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"%@",@"会不会到这里来了");
switch (status) {
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用户未决定");
break;
}
case kCLAuthorizationStatusDenied:
{
// 判断当前设备是否支持定位, 定位服务是否开启
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"真正被拒绝");
// 提醒给APP 授权
// iOS8.0- , 截图
// iOS8.0+ , 通过调用一个方法, 来直接到达设置界面
// 跳转核心代码
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
//[[UIApplication sharedApplication] openURL: url];
NSDictionary* dict=[[NSDictionary alloc] init];
[[UIApplication sharedApplication] openURL:url options:dict completionHandler:^(BOOL success) {
}];
}
}
else
{
NSLog(@"定位服务被关闭");
}
break;
}
// 系统预留字段
case kCLAuthorizationStatusRestricted:
{
NSLog(@"受限制");
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
{
NSLog(@"前后台定位授权");
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"前台定位授权");
break;
}
default:
break;
}
}
3. 定位回调:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"%@",@"定位到了");
/**
coordinate : 经纬度 CLLocationCoordinate2D
altitude : 海拔
horizontalAccuracy : 如果是负值, 代表当前位置数据不可用
verticalAccuracy : 海拔 -- 如果是负值, 代表当前海拔数据不可用
course : 航向 (0.0----359.9)
speed : 速度
floor : 楼层
方法:
distanceFromLocation : 计算两个坐标之间的物理直线距离
*/
CLLocation* location1= [locations lastObject];
CLLocationCoordinate2D coordinate = location1.coordinate;
NSLog(@"经度:%f,纬度:%f",coordinate.longitude,coordinate.latitude);
// 2.反地理编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
// 如果有错误,或者解析出来的地址数量为0
if (placemarks.count == 0 || error){
NSLog(@"%@",@"该经纬度没有找到对应地址");
return ;
}
// 取出地标,就可以取出地址信息,以及CLLocation对象
CLPlacemark *pm = [placemarks firstObject];
/*
@property (nonatomic, readonly, copy, nullable) NSString *name; // eg. Apple Inc. 工业路24号
@property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; // street name, eg. Infinite Loop 工业路
@property (nonatomic, readonly, copy, nullable) NSString *subThoroughfare; // eg. 1 24号
@property (nonatomic, readonly, copy, nullable) NSString *locality; // city, eg. Cupertino 深圳市
@property (nonatomic, readonly, copy, nullable) NSString *subLocality; // neighborhood, common name, eg. Mission District 宝安区
@property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; // state, eg. CA 广东省
@property (nonatomic, readonly, copy, nullable) NSString *subAdministrativeArea; // county, eg. Santa Clara
@property (nonatomic, readonly, copy, nullable) NSString *postalCode; // zip code, eg. 95014
@property (nonatomic, readonly, copy, nullable) NSString *ISOcountryCode; // eg. US
@property (nonatomic, readonly, copy, nullable) NSString *country; // eg. United States 中国
@property (nonatomic, readonly, copy, nullable) NSString *inlandWater; // eg. Lake Tahoe
@property (nonatomic, readonly, copy, nullable) NSString *ocean; // eg. Pacific Ocean
*/
#warning 注意:如果是取出城市的话,需要判断locality属性是否有值(直辖市时,该属性为空)
NSLog(@"name:%@,城市是:%@--城市2:%@,街道是:%@--%@,bbbb---%@,sublocality:%@",pm.name,pm.locality,pm.administrativeArea,pm.thoroughfare,pm.subLocality,pm.subThoroughfare
,pm.country);
if (pm.locality) {
NSLog(@"城市是:%@",pm.locality);
} else {
NSLog(@"城市是:%@",pm.administrativeArea);
}
}];
// [manager stopUpdatingLocation];
}
// 定位失败时调用
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"定位失败");
}
4. 手动开启定位:
[self.locationM requestLocation];
// 后台定位
// [self.locationM startUpdatingLocation];
5. 申请权限info.plist中
*********************后台定位要求
1 . info.plist中配置:Privacy - Location Always and When In Use Usage Description
2. Java代码请求: [_locationM requestWhenInUseAuthorization];
_locationM.allowsBackgroundLocationUpdates = YES;
3. xcode -> Capabilities -> Background Modes - Location updata中勾选
4. [self.locationM startUpdatingLocation];开启定位
后台定位标志: