开发 iOS 定位SDK 概述

概述 最后更新时间: 2025年12月03日

重要:由于个人信息保护法的实施,从定位2.8.0版本起对旧版本SDK不兼容,请务必确保调用SDK任何接口前先调用更新隐私合规updatePrivacyShow、updatePrivacyAgree两个接口,否则可能导致功能不可用等异常情况。具体可参考开发指南-隐私合规说明 传入相关参数。

简介

高德 iOS 定位 SDK 提供了不依赖于地图定位的定位功能,开发者可以无地图显示的场景中便捷地为应用程序添加定位功能。

iOS定位SDK提供了单次定位、连续定位、逆地理信息、地理围栏等功能。

面向的读者

高德地图定位 SDK 是提供给具有一定 iOS 编程经验,了解面向对象编程概念的读者使用的 iOS 移动端 SDK。

功能介绍与体验

  • 基础定位
    - (void)configLocationManager
    {
        self.locationManager = [[AMapLocationManager alloc] init];
    
        [self.locationManager setDelegate:self];
    
        [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
        [self.locationManager setAllowsBackgroundLocationUpdates:YES];
    }
    
    - (void)startSerialLocation
    {
    	//开始定位
        [self.locationManager startUpdatingLocation];
    }
    
    - (void)stopSerialLocation
    {
    	//停止定位
        [self.locationManager stopUpdatingLocation];
    }
    
    - (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
    {
    	//定位错误
        NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
    }
    
    - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
    {
    	//定位结果
        NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    }
  • 逆地理编码
    - (void)configLocationManager
    {
        self.locationManager = [[AMapLocationManager alloc] init];
    
        [self.locationManager setDelegate:self];
    
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
        [self.locationManager setLocationTimeout:6];
    
        [self.locationManager setReGeocodeTimeout:3];
    }
    
    - (void)locateAction
    {
    	//带逆地理的单次定位
        [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
    
            if (error)
            {
                NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
    
                if (error.code == AMapLocationErrorLocateFailed)
                {
                    return;
                }
            }
    
            //定位信息
            NSLog(@"location:%@", location);
    
            //逆地理信息
            if (regeocode)
            {
                NSLog(@"reGeocode:%@", regeocode);
            }
        }];
    }
  • 地理围栏
    - (void)configLocationManager
    {
        self.locationManager = [[AMapLocationManager alloc] init];
    
        [self.locationManager setDelegate:self];
    
        [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
        [self.locationManager setAllowsBackgroundLocationUpdates:YES];
    }
    
    - (void)addCircleReionForCoordinate:(CLLocationCoordinate2D)coordinate
    {
        int radius = 250;
    
        //创建circleRegion
        AMapLocationCircleRegion *cirRegion = [[AMapLocationCircleRegion alloc] initWithCenter:coordinate
                                                                                        radius:radius
                                                                                    identifier:@"circleRegion"];
    
        //添加地理围栏
        [self.locationManager startMonitoringForRegion:cirRegion];
    
        //保存地理围栏
        [self.regions addObject:cirRegion];
    
        //添加Overlay
        MACircle *circle = [MACircle circleWithCenterCoordinate:coordinate radius:radius];
        [self.mapView addOverlay:circle];
        [self.mapView setVisibleMapRect:circle.boundingMapRect];
    }
    
    - (void)amapLocationManager:(AMapLocationManager *)manager didStartMonitoringForRegion:(AMapLocationRegion *)region
    {
        NSLog(@"开始监听地理围栏:%@", region);
    }
    
    - (void)amapLocationManager:(AMapLocationManager *)manager monitoringDidFailForRegion:(AMapLocationRegion *)region withError:(NSError *)error
    {
        NSLog(@"监听地理围栏失败:%@", error.localizedDescription);
    }
    
    - (void)amapLocationManager:(AMapLocationManager *)manager didEnterRegion:(AMapLocationRegion *)region
    {
        NSLog(@"进入地理围栏:%@", region);
    }
    
    - (void)amapLocationManager:(AMapLocationManager *)manager didExitRegion:(AMapLocationRegion *)region
    {
        NSLog(@"退出地理围栏:%@", region);
    }

下载完整示例代码

账号与Key的申请

注册成为高德开发者需要分三步:

第一步,注册高德开发者;

第二步,去控制台创建应用;

第三步,获取Key。

具体步骤可参看下图

获取 API Key

兼容性

高德地图 iOS 定位 SDK V2.2.0 之前的版本兼容 iOS 6.0 系统,从 V2.2.0 版本以后从 iOS 7.0 系统开始支持。SDK 内部网络访问全部应用域名访问,不涉及 ipv4 和 ipv6 的问题。

开发者使用注意事项

法人或非法人组织使用平台服务应事先购买技术服务许可以获取许可。若您未购买技术服务许可,平台向您提供的KEY和服务配额仅供您用于短期、少量的测试目的;若您超出前述范围使用平台服务(包括但不限于您开始向第三方或公众销售或提供您的应用、将您的应用用于参与第三方投标、您的应用在应用程序分发平台上架或已经可以被公众获取使用、您的应用开始收费或发布广告或以其他方式获益、在组织内部使用的您的应用已上线运营、您的应用长期或大量调用平台服务等情形),构成未许可使用,平台有权采取相应措施。关于具体使用规则,请参阅《高德地图开放平台服务协议》获得详细信息。 

返回顶部 示例中心 常见问题 智能客服 公众号
二维码