高德地圖,獲取當(dāng)前位置經(jīng)緯度

1. 導(dǎo)入頭文件

#import ?<AMapFoundationKit/AMapFoundationKit.h>

#import <AMapLocationKit/AMapLocationKit.h>

2.聲明屬性

@property (nonatomic,strong) AMapLocationManager * locationManager;

3.初始化

#pragma mark - 懶加載

-(AMapLocationManager *)locationManager{

if (!_locationManager) {

_locationManager = [[AMapLocationManager alloc]init];

}

return _locationManager;

}


4.定位,獲得位置和坐標(biāo)

#pragma mark - 定位

- (void)location{

?self.locationManager.delegate = self;

//高精度定位

// 帶逆地理信息的一次定位(返回坐標(biāo)和地址信息)

[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

//? 定位超時(shí)時(shí)間,最低2s,此處設(shè)置為10s

self.locationManager.locationTimeout =10;

//? 逆地理請(qǐng)求超時(shí)時(shí)間,最低2s,此處設(shè)置為10s

self.locationManager.reGeocodeTimeout = 10;

//獲得返回的地址

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

????????//得到定位的經(jīng)緯度

????????CLLocationDegrees latitude = location.coordinate.latitude;

????????CLLocationDegrees longitude = location.coordinate.longitude;

????????NSLog(@"經(jīng)度%f:",longitude);

????????NSLog(@"緯度%f:",latitude);

????????if (regeocode)

????????{

????????????????????NSLog(@"reGeocode:%@", regeocode);

????????????????????NSLog(@"reGeocode.cityName:%@", regeocode.city);

????????????}

????}];

}


5.也可以在代理方法中獲得定位度

#pragma mark - AMapLocationManagerDelegate 協(xié)議

#pragma mark - 定位失敗

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error

{

NSLog(@"定位失敗");

}

#pragma mark - 定位成功

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location

{

[_locationManager stopUpdatingLocation];

NSLog(@"高德地圖定位的經(jīng)緯度:%f %f",location.coordinate.latitude,location.coordinate.longitude);

//? ? [self HttpCityLatitude:location.coordinate.latitude Longitude:location.coordinate.longitude];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容