ios手机定位 -- 高德地图

需要添加的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);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值