需要添加的Plist文件
LSApplicationQueriesSchemes (类型Array : 里面存一个元素 NSString类型 iosamap)
NSLocationWhenInUseUsageDescription(NSString类型 :后面写要提示的信息)
//需要用pod导入的第三方
需要签订的协议:<CLLocationManagerDelegate,AMapSearchDelegate>
//定义
CLLocationManager *clManager;//用于获取位置的类
CLLocation *currentLocation;//保存位置的信息的类
AMapSearchAPI *search;//搜索类
AMapSearchServices *service;
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
[lm startUpdatingLocation];
//询问是否允许获取当前位置
[lm requestWhenInUseAuthorization];
// 高德地图请求 对象 单例模式初始化
services = [AMapSearchServices sharedServices];
services.apiKey = @"dbaebeb01598470926382e1a5d5d1268";//自己获取高德的apiKey
//你地理搜索 对象
search = [[AMapSearchAPI alloc] init];
search.delegate = self;
#pragma mark- CLLocationManagerDelegate回调的函数
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//用于保存当前位置信息的对象 -传过来的数组 是每次定位后的存坐标的数组 最后一个对象 就是当前位置
CLLocation *location = [locations lastObject];
//将 当前位置的对象 赋值给 自己定义的,为了下一个回调函数使用
currentLocation = location;
[self reGoAction];
[lm stopUpdatingLocation];
}
- (void)reGoAction {
if (currentLocation) {
//初始化一个逆地理编码的请求对象
AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
// 通过currentLocation的经纬度初始化point 将当前位置对象赋值给高德地址对象
AMapGeoPoint *point = [AMapGeoPoint locationWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
//将高德地址对象 赋值给你地理编码的请求对象
request.location = point;
request.requireExtension = YES;
//逆地址编码查询接口
[search AMapReGoecodeSearch:request];
}
}
#pragma mark - 逆地理搜索回调
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
if (response.regeocode) {//response:逆地理编码结果返回 regeocode:逆地理编码结果
//addressComponent:地址组成要素
NSString *temp1=response.regeocode.addressComponent.province;
NSString *temp=response.regeocode.addressComponent.city;
NSString *temp2=response.regeocode.addressComponent.district;
NSString *strRe=[NSString stringWithFormat:@"%@,%@,%@",temp,temp1,temp2];
NSLog(@"%@",strRe);
}
}
- (void)searchRequest:(id)request didFailWithError:(NSError *)error {
NSLog(@"%@",error);
}