天天看點

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