天天看点

ios 高地地图实现定位

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

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

ios 高地地图实现定位

3.ios工程配置:

Info.plist

ios 高地地图实现定位

添加配置:

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

导入支持库:

ios 高地地图实现定位

定位代码:

引入头文件:

#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);
}