行政区划
1. 导入头文件
#import <NavinfoKit/NavinfoKit.h>
2. 配置APIKEY
参考工程配置说明。
3. 定义 NavinfoDistrictSearch
定义路况搜索对象 NavinfoDistrictSearch,并继承搜索协议<NavinfoDistrictDelegate>。
4. 构造 NavinfoDistrictSearch
构造事件搜索对象 NavinfoDistrictSearch,并设置代理。
_distSearch = [NavinfoDistrictSearch new]; _distSearch.delegate = self;
5. 设置行政区搜索查询参数
道路模糊搜索查询的请求参数类为 NavinfoDistrictQuery,keyword(关键字)为非必填写参数,搜索关键词支持:行政区名称、code、adcode、subDistrictCount(子级行政区)。设置显示下级行政区级数;hasExtensionInfo(行政区边界)控制行政区边界坐标点是否返回。其它参数请参考API文档。
NavinfoDistrictQuery *query = [NavinfoDistrictQuery new]; query.keyword = @"辽宁"; query.subDistrictCount = 1; query.hasExtensionInfo = NO;
6. 发起搜索查询参数
通过调用 NavinfoDistrictQuery的 districtSearchWithQuery方法发起搜索请求。
[_distSearch districtSearchWithQuery:query];
7. 在回调中处理数据
当查询成功时,代理方法。
- (void)onDistrictSearch:(NavinfoDistrictSearch *)search result:(NavinfoDistrictResult * _Nullable)result error:(NSError * _Nullable)error;
其中NavinfoDistrictResult返回,NSError 为nil,此时可获取查询结果。
说明:
通过 districts 获NavinfoBaseDistrict类型集合。
- (void)onDistrictSearch:(NavinfoDistrictSearch *)search result:(NavinfoDistrictResult *)result error:(NSError *)error { NSLog(@"----> onDistrictSearch"); if (result.districts) { NavinfoBaseDistrict *dist = result.districts[0]; NSString *adcode = dist.adcode; NSString *cityCode = dist.cityCode; NSString *name = dist.name; NavinfoLonlat *lonlat = dist.center; NSString *center = [lonlat lonlatString]; NSString *lonlats = dist.lonlats; NSString *level = dist.level; NSArray *dists = dist.districts; NSLog(@"----> result.adcode=%@, result.cityCode=%@, result.name=%@, result.center= %@, result.lonlats=%@, result.level=%@",adcode,cityCode,name,center,lonlats,level); for (NSInteger i = 0; i < [dists count]; i++) { NSLog(@"----> subdist.adcode=%@, subdist.cityCode=%@, subdist.name=%@, subdist.lonlats=%@, subdist.level=%@",dist.districts[i].adcode,dist.districts[i].cityCode,dist.districts[i].name,dist.districts[i].lonlats,dist.districts[i].level); } } }
8. 处理失败查询 当检索失败时,Error 会返回,通过该回调函数获取产生的失败的原因。