iOS8 定位问题 ios无法定位
在IOS8中定位功能新增了两个方法:
- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
这两个新增的方法导致,之前写的程序在iOS8运行会出现,定位功能无法正常使用
这样让iOS8正常使用定位功能呢?
<1>你需要在info.plist表里面添加两条变量
在Info.plist中加入两个缺省没有的字段
-
NSLocationAlwaysUsageDescription
-
NSLocationWhenInUseUsageDescription
这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语。
然后调用下边的方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[MBProgressHUD hideHUD];
// 数组里面存放的是CLLocation对象, 一个CLLocation就代表一个位置
CLLocation *loc = [locations lastObject];
// 纬度:loc.coordinate.latitude
// 经度:loc.coordinate.longitude
// NSLog(@"纬度=%f, 经度=%f", loc.coordinate.latitude, loc.coordinate.longitude);
QKYLog(@"%@",loc.description);
CLLocationDegrees latitude = loc.coordinate.latitude;
CLLocationDegrees longtitude = loc.coordinate.longitude;
self.latitude=[NSString stringWithFormat:@"%f",latitude];
self.longitude=[NSString stringWithFormat:@"%f",longtitude];
// 开始反向编码
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error || placemarks.count == 0) {
[MBProgressHUD showError:@"您的位置在火星上..."];
} else { // 编码成功(找到了具体的位置信息)
// 输出查询到的所有地标信息
for (CLPlacemark *placemark in placemarks) {
QKYLog(@"+++++++%@",placemark.addressDictionary[@"Name"]);
// self.position.text = placemark.addressDictionary[@"Name"];
NSString *str = placemark.addressDictionary[@"Name"];
// NSMutableDictionary *contentdic = [NSMutableDictionary dictionary];
// contentdic[NSFontAttributeName] = [UIFont systemFontOfSize:13.0];
//
// CGRect rect = [str boundingRectWithSize:CGSizeMake(QKYScreenW -20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:contentdic context:nil];
// CGSize textSize = rect.size;
[self.position setTitle:str forState:UIControlStateNormal];
// self.positionBtn.width = textSize.width;
// self.positionBtn.height = textSize.height+20;
// self.position.hidden = NO;
// self.position.y = CGRectGetMaxY(self.positionBtn.frame)+5;
}
}
}];
// 停止更新位置(不用定位服务,应当马上停止定位,非常耗电)
[manager stopUpdatingLocation];
}

浙公网安备 33010602011771号