iOS定位实现

xiaoxiao2021-02-28  144

导入框架
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) { // 提示用户出错原因, 可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在 } }
注意事项
/** * 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>
基本实现
// MARK: - 开启定位 - (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 } } // MARK: - CLLocationManangerDelegate // 定位成功以后调用 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { [self.locationManager stopUpdatingLocation]; CLLocation *location = locations.lastObject; [self reverseGeocoder:location]; } // MARK: - Geocoder // 反地理编码 - (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 } }]; }
转载请注明原文地址: https://www.6miu.com/read-45655.html

最新回复(0)