导入框架
Xcode中添加 "CoreLocation.framework"
导入头文件
#import <CoreLocation/CoreLocation.h>
声明管理器和代理
@interface LocationViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
初始化管理器
_locationManager = [[CLLocationManager alloc] init]
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyBest
开启定位服务 需要定位时调用currentLocation方位
- (void)currentLocation {
/**
* 由于IOS8中定位的授权机制改变 需要进行手动授权
* 获取授权认证,两个方法:
* [self.locationManager requestWhenInUseAuthorization];
* [self.locationManager requestAlwaysAuthorization];
*/
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] {
NSLog(@"requestAlwaysAuthorization");
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
}
代理设置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
}
}
注意事项
/**
* iOS 8 添加下面的一些字段
*/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>访问位置</string>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>始终访问位置</string>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>访问位置</string>
</plist>
基本实现
- (void)applicationLocationManager {
if ([CLLocationManager locationServicesEnabled]) {
if (!_locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:100];
[self.locationManager startUpdatingLocation];
[self.locationManager startUpdatingHeading];
}
}
else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil
message:@"您没有开启定位功能"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:@"取消", nil];
[alertView show];
#pragma clang diagnostic pop
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[self.locationManager stopUpdatingLocation];
CLLocation *location = locations.lastObject;
[self reverseGeocoder:location];
}
- (void)reverseGeocoder:(CLLocation *)currentLocation {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error || placemarks.count == 0) {
}
else {
CLPlacemark *placemark = placemarks.firstObject;
NSLog(@"placemark:%@", [[placemark addressDictionary] objectForKey:@"City"]);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你的位置" message:[[placemark addressDictionary] objectForKey:@"City"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
#pragma clang diagnostic pop
}
}];
}