1. 导入头文件
#import
#import
2.声明属性
@property (nonatomic,strong) AMapLocationManager * locationManager;
3.初始化
#pragma mark - 懒加载
-(AMapLocationManager *)locationManager{
if (!_locationManager) {
_locationManager = [[AMapLocationManager alloc]init];
}
return _locationManager;
}
4.定位,获得位置和坐标
#pragma mark - 定位
- (void)location{
self.locationManager.delegate = self;
//高精度定位
// 带逆地理信息的一次定位(返回坐标和地址信息)
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// 定位超时时间,最低2s,此处设置为10s
self.locationManager.locationTimeout =10;
// 逆地理请求超时时间,最低2s,此处设置为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);
//得到定位的经纬度
CLLocationDegrees latitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;
NSLog(@"经度%f:",longitude);
NSLog(@"纬度%f:",latitude);
if (regeocode)
{
NSLog(@"reGeocode:%@", regeocode);
NSLog(@"reGeocode.cityName:%@", regeocode.city);
}
}];
}
5.也可以在代理方法中获得定位度
#pragma mark - AMapLocationManagerDelegate 协议
#pragma mark - 定位失败
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"定位失败");
}
#pragma mark - 定位成功
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
[_locationManager stopUpdatingLocation];
NSLog(@"高德地图定位的经纬度:%f %f",location.coordinate.latitude,location.coordinate.longitude);
// [self HttpCityLatitude:location.coordinate.latitude Longitude:location.coordinate.longitude];
}