先添加一个搜索框:
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 44)];
_searchBar.delegate = self;
[self.view addSubview:_searchBar];
简单的apikey自己在高德申请
[AMapSearchServices sharedServices].apiKey = MAP_KEY;
//初始化检索对象_searchArr = [[AMapSearchAPI alloc] init];
_searchArr.delegate = self;
然后执行搜索框的代理方法:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"%f",[UIApplication sharedApplication].keyWindow.frame.size.height);
flagSearched = NO;
return YES;
}
//输入提示的搜索 这是重点 因为提示的时候是在你输入变化的时候进行数据提示的
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
AMapInputTipsSearchRequest *tipsSearch = [[AMapInputTipsSearchRequest alloc] init];
_collectionV.hidden = YES;
if (searchBar.text.length < 1) {
_collectionV.hidden = NO;
}
//搜索的关键字:
tipsSearch.keywords = searchText;
.//这个是搜索的限制城市:
tipsSearch.city = self.parkCity;
//搜索的关键代码然后会执行搜索回调:见下面的代理方法!
[_searchArr AMapInputTipsSearch:tipsSearch];
}
/* 输入提示回调. */
- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
{
[self.arrResults setArray:response.tips];
[self.tbSearchResults reloadData];
}
AMapInputTipsSearchResponse:有两个属性:
@property (nonatomic, assign) NSInteger count; //!< 返回数目
@property (nonatomic, strong) NSArray *tips; //!< 提示列表 AMapTip 数组
AMapTip:
@property (nonatomic, copy) NSString *uid; //!< poi的id
@property (nonatomic, copy) NSString *name; //!< 名称
@property (nonatomic, copy) NSString *adcode; //!< 区域编码
@property (nonatomic, copy) NSString *district; //!< 所属区域
@property (nonatomic, copy) AMapGeoPoint *location; //!< 位置