ios 高地地图实现定位

1. http://lbs.amap.com/ 注册开发账号、应用账号获取key

2. http://lbs.amap.com/api/ios-location-sdk/download下载开发包,两个都需要


3.ios工程配置:

Info.plist

添加配置:

<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>亲,需要开启定位,可以吗?</string>
<key>UIBackgroundModes</key>
<array>
	<string>location</string>
</array>

导入支持库:


定位代码:

引入头文件:

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
@interface RNAmapLocation()<AMapLocationManagerDelegate>

@property (nonatomic, strong) AMapLocationManager *locationManager;
@property (nonatomic, copy)   AMapLocatingCompletionBlock completionBlock;

@end


- (instancetype)init
{
    self = [super init];
    if (self) {
        [AMapServices sharedServices].apiKey = @"*****(高德开发者网站注册获取的key)";
        [self initCompleteBlock];
    }
    return self;
}

- (void)startSerialLocation
{
    //开始定位
  //    [self.locationManager startUpdatingLocation];//连续定位
    //单次定位
    self.locationManager.locationTimeout = 10;
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
   //开启逆定位可以返回具体地址和名称
}

- (void)stopSerialLocation
{
    //停止定位
    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDelegate:nil];
}

#pragma mark - Initialization

- (void)initCompleteBlock
{
    //__weak RNAmapLocation *weakSelf = self;
    self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
    {
        if (error != nil && error.code == AMapLocationErrorLocateFailed)
        {
            //定位错误:此时location和regeocode没有返回值,不进行annotation的添加
            NSLog(@"定位错误:{%ld - %@};", (long)error.code, error.localizedDescription);
            [weakSelf.bridge.eventDispatcher sendAppEventWithName:@"locationError" body:nil];
            return;
        }
        else if (error != nil
                 && (error.code == AMapLocationErrorReGeocodeFailed
                     || error.code == AMapLocationErrorTimeOut
                     || error.code == AMapLocationErrorCannotFindHost
                     || error.code == AMapLocationErrorBadURL
                     || error.code == AMapLocationErrorNotConnectedToInternet
                     || error.code == AMapLocationErrorCannotConnectToHost))
        {
            //逆地理错误:在带逆地理的单次定位中,逆地理过程可能发生错误,此时location有返回值,regeocode无返回值,进行annotation的添加
            NSLog(@"逆地理错误:{%ld - %@};", (long)error.code, error.localizedDescription);
            return;
        }
        else if (error != nil && error.code == AMapLocationErrorRiskOfFakeLocation)
        {
            //存在虚拟定位的风险:此时location和regeocode没有返回值,不进行annotation的添加
            NSLog(@"存在虚拟定位的风险:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            return;
        }
        else
        {
            //没有错误:location有返回值,regeocode是否有返回值取决于是否进行逆地理操作,进行annotation的添加
           NSDictionary *body = @{@"formattedAddress": regeocode.formattedAddress ,
                                  @"country":regeocode.country,
                                  @"province":regeocode.province,
                                  @"city":regeocode.city,
                                  @"district":regeocode.district,
                                  @"POIName":regeocode.POIName,
                                  @"AOIName":regeocode.AOIName,};
        }
      
    };
}


#pragma mark - AMapLocationManagerDelegate

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    //定位错误
    NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    //定位结果
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值