坏消息:如果不适配iOS9,就不能偷偷在后台定位,好消息:将允许出现这种场景:同一App中的多个location manager:一些只能在前台定位,另一些可在后台定位,并可随时开启或者关闭特定location manager的后台定位。
需要配置info.plist
Required background modes App registers for location updates
XML是:
iOS8自定义提示用户授权使用地理定位功能时的提示语
需要在Info.plist增加一个键值NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription
Core Location framework的变化主要有以下几点:
1. 在定位状态中引入Always 和WhenInUse的概念。
2. 加入Visit monitoring的特性, 这类特性特别适合旅行类别的应用,当用户到达某个指定的区域内,monitor开始作用。
3.加入室内定位技术,增加CLFloor, 在室内可以得到楼层信息。
简单获得经纬度:
//先初始化
CLLocationManager *locMgr;
locMgr = [[CLLocationManageralloc] init];
CLAuthorizationStatus status = [CLLocationManagerauthorizationStatus];
//判断有没有权限
if (kCLAuthorizationStatusDenied == status ||kCLAuthorizationStatusRestricted == status) {
//iOS8可以可以打开设置
//[[UIApplicationsharedApplication] openURL:[NSURLURLWithString: UIApplicationOpenSettingsURLString]];
if (_IOS8) {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:nilmessage:@"沒有權限獲取位置"delegate:selfcancelButtonTitle:@"設置"otherButtonTitles:@"確定",nil];
alert.tag=201;
[alert show];
}else{
[clsOtherFunShowMsg:@"沒有權限獲取位置"];
}
}else{
[locMgrsetDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
if (_IOS8) {
locMgr.distanceFilter =kCLDistanceFilterNone;
}
locMgr.delegate =self;
[locMgrstartUpdatingLocation];
}
#pragma mark -CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if (error.code==kCLErrorDenied) {
NSLog(@"error.code,%ld",error.code);
}
}
// 6.0 以上调用这个函数
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
casekCLAuthorizationStatusNotDetermined:
if ([managerrespondsToSelector:@selector(requestAlwaysAuthorization)]) {
[manager requestWhenInUseAuthorization];
}
break;
default:
break;
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[locMgrstopUpdatingLocation];
CLLocation *newLocation = locations[0];
CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
NSTimeInterval locationAge = -[newLocation.timestamptimeIntervalSinceNow];
NSLog(@"%f", locationAge);
NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
//------------------位置反编码---5.0之后使用-----------------
CLGeocoder *geocoder = [[CLGeocoderalloc] init];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks,NSError *error){
for (CLPlacemark *placein placemarks) {
NSLog(@"name,%@",place.name); // 位置名
// NSLog(@"thoroughfare,%@",place.thoroughfare); //街道
// NSLog(@"subThoroughfare,%@",place.subThoroughfare); //子街道
// NSLog(@"locality,%@",place.locality); //市
// NSLog(@"subLocality,%@",place.subLocality); //区
// NSLog(@"country,%@",place.country); //国家
}
}];
}
//判断是不是在中国
+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location
{
if (location.longitude <72.004 || location.longitude >137.8347 || location.latitude <0.8293 || location.latitude >55.8271)
returnYES;
returnNO;
}
//转换为中国坐标
+(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
{
CLLocationCoordinate2D adjustLoc;
if([selfisLocationOutOfChina:wgsLoc]){
adjustLoc = wgsLoc;
}else{
double adjustLat = [selftransformLatWithX:wgsLoc.longitude -105.0 withY:wgsLoc.latitude -35.0];
double adjustLon = [selftransformLonWithX:wgsLoc.longitude -105.0 withY:wgsLoc.latitude -35.0];
double radLat = wgsLoc.latitude /180.0 * pi;
double magic =sin(radLat);
magic = 1 -ee * magic * magic;
double sqrtMagic =sqrt(magic);
adjustLat = (adjustLat * 180.0) / ((a * (1 -ee)) / (magic * sqrtMagic) * pi);
adjustLon = (adjustLon * 180.0) / (a / sqrtMagic *cos(radLat) * pi);
adjustLoc.latitude = wgsLoc.latitude + adjustLat;
adjustLoc.longitude = wgsLoc.longitude + adjustLon;
}
return adjustLoc;
}
+(double)transformLatWithX:(double)x withY:(double)y
{
double lat = -100.0 +2.0 * x + 3.0 * y +0.2 * y * y + 0.1 * x * y +0.2 * sqrt(abs(x));
lat += (20.0 *sin(6.0 * x *pi) + 20.0 *sin(2.0 * x *pi)) * 2.0 /3.0;
lat += (20.0 *sin(y * pi) +40.0 * sin(y /3.0 * pi)) *2.0 / 3.0;
lat += (160.0 *sin(y / 12.0 *pi) + 320 *sin(y * pi /30.0)) * 2.0 /3.0;
return lat;
}
+(double)transformLonWithX:(double)x withY:(double)y
{
double lon =300.0 + x + 2.0 * y +0.1 * x * x + 0.1 * x * y +0.1 * sqrt(abs(x));
lon += (20.0 *sin(6.0 * x *pi) + 20.0 *sin(2.0 * x *pi)) * 2.0 /3.0;
lon += (20.0 *sin(x * pi) +40.0 * sin(x /3.0 * pi)) *2.0 / 3.0;
lon += (150.0 *sin(x / 12.0 *pi) + 300.0 *sin(x / 30.0 *pi)) * 2.0 /3.0;
return lon;
}