iOS开发地图定位及地理编码

在iOS8及以上版本进行地图定位服务时,需在Info.plist设置定位权限描述。通过requestAlwaysAuthorization和requestWhenInUseAuthorization获取用户授权。开发中引入相关工具包,并考虑在高德地图上进行坐标测试以减小误差。地理编码用于将位置转换为经纬度,反地理编码则将经纬度转换为地名,CLGeocoder是实现这一功能的类,但同一实例仅能执行一次操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     iOS8以后要使用地图定位服务,必须在 Info.plist 文件中配置 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 属性来告诉用户使用定位服务的目的

     值为告知用户的提示信息。

     使用定位前还需要使用 requestAlwaysAuthorizationrequestWhenInUseAuthorization 方法获得用户的允许

自工程中需要导入定位所需的工具包

苹果默认的地图是高德地图,所以我们在开发测试的时候也尽量在高德地图上取坐标进行测试(坐标拾取工具),这样会使测试误差减小

#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"]);
    }];
    
}
打印结果如下:











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值