iOS使用高德的正确姿势(一)

高德-2.png

首先得有个高德APPkey

集成我是Pod一步到位贼轻松

  1. 为啦项目上架不被拒长远考虑
  2. 避免一不小心更新版本带来的烦恼
  3. plist文件配置


    plist配置.png

建议 podfiled 文件这么搞

pod 'AMap3DMap-NO-IDFA', '~> 6.2.0'

pod 'AMapSearch-NO-IDFA' ,  '~>6.1.1'

pod 'AMapLocation-NO-IDFA' , '~>2.6.0'

开始实现 定位+附近搜索 + 输入关键字搜索

1.导入头文件

//高德
#import 
#import 
#import 
//高德apiKEY
#define GDAPIKEY @"88a277eea795400dd56e9c87e4f96ba8"
//遵守协议

2.初始化

//高德key 初始化
    [AMapServices sharedServices].apiKey =GDAPIKEY;
    self.locationManager = [[AMapLocationManager alloc] init];
    self.locationManager.delegate = self;
    //搜索api初始化
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    // 带逆地理信息的一次定位(返回坐标和地址信息)
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //   定位超时时间,最低2s,此处设置为2s
    self.locationManager.locationTimeout =2;
    //   逆地理请求超时时间,最低2s,此处设置为2s
    self.locationManager.reGeocodeTimeout = 2;

  //自定义搜索框
    HWSearchBar *searchbar=[HWSearchBar searchBar];
  searchbar.frame=CGRectMake(10, 10, kScreenWidth-20, 40);
    [view1 addSubview:searchbar];
    /* 监听 TextField 的输入(内容改变就会调用) */
    [[searchbar rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"--输入内容--%@", x);
        
        //发起输入提示搜索
        AMapInputTipsSearchRequest *tipsRequest = [[AMapInputTipsSearchRequest alloc] init];
        //关键字
        tipsRequest.keywords = x;
        //城市
        //    tipsRequest.city = _currentCity;
        
        //执行搜索
        [self.search AMapInputTipsSearch: tipsRequest];
        
    }];

3.实现方法

#pragma -mark —————————Action————————
//周边搜索 实现
- (void)locationBtnAction{
    // 带逆地理(返回坐标和地址信息)。将下面代码中的 YES 改成 NO ,则不会返回地址信息。
    [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;
            }
        }
        /**location 当前定位地理信息*/
        NSLog(@"location:%@", location);
        self.location=location;
        [self searchAround];
        //当前位置逆地理信息
        if (regeocode)
        {
            NSLog(@"reGeocode:%@", regeocode);
        }
        
    }];
}
/** 根据定位坐标进行周边搜索 */
- (void)searchAround{
    
    //构造AMapPOIAroundSearchRequest对象,设置周边请求参数
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    request.location = [AMapGeoPoint locationWithLatitude:self.location.coordinate.latitude longitude:self.location.coordinate.longitude];
    // types属性表示限定搜索POI的类别,默认为:餐饮服务|商务住宅|生活服务
    // POI的类型共分为20种大类别,分别为:
    // 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|
    // 医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|
    // 交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
    request.types = @"汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施";
    request.sortrule = 0;
    request.requireExtension = YES;
    
    NSLog(@"周边搜索");
    
    //发起周边搜索
    [self.search AMapPOIAroundSearch: request];
}
// 实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
    NSLog(@"周边搜索回调");
    if(response.pois.count == 0)
    {
        return;
    }
    
    self.dataArray = [NSMutableArray arrayWithArray:response.pois];
    // 周边搜索完成后,刷新tableview
    [self.tableView reloadData];  
}
//实现输入提示的回调函数
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest*)request response:(AMapInputTipsSearchResponse *)response
{
    if(response.tips.count == 0)
    {
        return;
    }
    //通过AMapInputTipsSearchResponse对象处理搜索结果
    //先清空数组
    [self.dataArray removeAllObjects];
    for (AMapTip *p in response.tips) {
        //把搜索结果存在数组
        [self.dataArray addObject:p];
    }
    //刷新表视图
    [self.tableView reloadData];
}

你可能感兴趣的:(iOS使用高德的正确姿势(一))