iOS8以后要使用地图定位服务,必须在 Info.plist 文件中配置 NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription 属性来告诉用户使用定位服务的目的
值为告知用户的提示信息。
使用定位前还需要使用 requestAlwaysAuthorization,requestWhenInUseAuthorization 方法获得用户的允许
自工程中需要导入定位所需的工具包
苹果默认的地图是高德地图,所以我们在开发测试的时候也尽量在高德地图上取坐标进行测试(坐标拾取工具),这样会使测试误差减小
#import "ViewController.h"
//导入定位所需工具包
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager *_manager;
}
@end
<span style="font-size:18px;">- (void)viewDidLoad {
[super viewDidLoad];
_manager = [[CLLocationManager alloc]init];
_manager.delegate = self;//开启代理,可以从代理中扑捉到当前的经纬度
[_manager requestAlwaysAuthorization];
[_manager requestWhenInUseAuthorization];
_manager.distanceFilter = 10;//更新频率,每隔多少米跟新一次
_manager.desiredAccuracy = kCLLocationAccuracyBest; //精准度,越高越耗电</span><pre name="code" class="objc"><pre name="code" class="objc"><span style="font-size:18px;"></span><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; line-height: 26px;"> /*</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size:18px;"><span style="font-family: Arial; line-height: 26px;"> </span> <a target=_blank href="file:///Users/rjxy/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS5_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/data/kCLLocationAccuracyBestForNavigation">kCLLocationAccuracyBestForNavigation</a> 最高精度,这种级别用于导航程序</span></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size:18px;"> <a target=_blank href="file:///Users/rjxy/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS5_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/data/kCLLocationAccuracyBest">kCLLocationAccuracyBest</a> 最高精度</span></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size:18px;"> <a target=_blank href="file:///Users/rjxy/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS5_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/data/kCLLocationAccuracyHundredMeters">kCLLocationAccuracyHundredMeters</a> 精度为100米内</span></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size:18px;"> <a target=_blank href="file:///Users/rjxy/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS5_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/data/kCLLocationAccuracyKilometer">kCLLocationAccuracyKilometer</a> 精度到公里范围内</span></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size:18px;"> <a target=_blank href="file:///Users/rjxy/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS5_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/data/kCLLocationAccuracyNearestTenMeters">kCLLocationAccuracyNearestTenMeters</a> 精度到10米内</span></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size:18px;"><span style="font-family:Arial;"><span style="line-height: 26px;"> </span></span><a target=_blank href="file:///Users/rjxy/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS5_1.iOSLibrary.docset/Contents/Resources/Documents/documentation/CoreLocation/Reference/CoreLocationConstantsRef/Reference/reference.html#//apple_ref/c/data/kCLLocationAccuracyThreeKilometers">kCLLocationAccuracyThreeKilometers</a> 精度到3公里范围内</span></p><span style="font-size:18px;"> */</span>
[_manager startUpdatingLocation]; //开启定位服务
[_manager startUpdatingHeading]; //开启设备朝向,模拟器不行
}
//代理CLLocationManagerDelegate的方法,可用于扑捉当前的经纬度和海拔高度
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *location = [locations lastObject];
//经纬度结构体:CLLocationCoordinate2D coordinate (CLLocationDegrees latitude 纬度;CLLocationDegrees longitude 经度)
//海拔高度:CLLocationDistance altitude
NSLog(@"\n纬度为:%f\n经度为:%f\n海拔高度为:%f",location.coordinate.latitude,location.coordinate.longitude,location.altitude);
}
//定位失败调用此方法
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
if ([error code] == kCLErrorDenied) {
NSLog(@"访问被拒绝");
}
if ([error code] == kCLErrorLocationUnknown) {
NSLog(@"无法获取位置信息");
}
}
//获取设备朝向信息
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
//地理朝向 //地磁朝向
NSLog(@"\n地磁朝向:%f\n地理朝向:%f",newHeading.magneticHeading,newHeading.trueHeading);
}
地理编码:
地理编码:知道位置换成经纬度
反地理编码:知道经纬度置换成地名
CLGeocoder 一个当前类对象只能执行一次操作
#import "ViewController.h"
//导入地图头文件,地理编码时需要用到
#import <MapKit/MapKit.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
CLGeocoder *_geocoder;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
_geocoder = [[CLGeocoder alloc]init];
//地理编码:
[_geocoder geocodeAddressString:@"温州博物馆" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placemark = [placemarks lastObject];
NSLog(@"\n搜索位置的经度为:%f\n搜索位置的纬度为%f\n搜索位置的海拔高度为%f",placemark.location.coordinate.longitude,placemark.location.coordinate.latitude,placemark.location.altitude);
}];
}
下面我们在协议里扑捉当前的位置信息,将其进行反地理编码
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *location = [locations lastObject];
//反地理编码
_geocoder = [[CLGeocoder alloc]init];
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placemark = [placemarks lastObject];
NSDictionary *dic = placemark.addressDictionary;
// NSLog(@"%@",dic);
NSLog(@"\n您当前位置为:%@",dic[@"Name"]);
}];
}
打印结果如下: