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