#import "ViewController2.h"
#import <AMapSearchKit/AMapSearchAPI.h>
@interface ViewController2 ()<AMapSearchDelegate,UITableViewDataSource,UITableViewDelegate>
{
UITextField *_textFile;
AMapSearchAPI *_search;
AMapPlaceSearchRequest *_poiRequest;
NSMutableArray *_dataArr;
UITableView *_myTableView;
}
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_dataArr=[[NSMutableArray alloc]init];
_textFile=[[UITextField alloc]initWithFrame:CGRectMake(20, 84, 300, 40)];
[email protected]"请输入搜索地址";
_textFile.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
_textFile.backgroundColor=[UIColor lightGrayColor];
//textFiled 也可以添加事件 UIControlEventEditingChanged这个是当textFiled发生改变的时候就触发
[_textFile addTarget:self action:@selector(textFileClick:) forControlEvents:UIControlEventEditingChanged];
[self.view addSubview:_textFile];
_myTableView=[[UITableView alloc]initWithFrame:CGRectMake(20, 150, 300, 300)];
_myTableView.delegate=self;
_myTableView.dataSource=self;
[self.view addSubview:_myTableView];
//初始化检索对象
_search=[[AMapSearchAPI alloc]initWithSearchKey:@"用户poiKey" Delegate:self];
//这是返回函数的返回内容的语言格式
_search.language=AMapSearchLanguage_zh_CN;
//构造AMapPlaceSearchRequest对象,配置关键字搜索参数要在下边出发事件里配置哟
_poiRequest =[[AMapPlaceSearchRequest alloc]init];
}
//这个就是地图检索函数的 返回函数
-(void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response
{
if (response.pois.count==0) {
return;
}
//通过AMapPlaceSearchResponse对象处理搜索结果
[_dataArr removeAllObjects];
for (AMapPOI *p in response.pois) {
[_dataArr addObject:p];
}
NSLog(@"Place: %ld", _dataArr.count);
[_myTableView reloadData];
}
//textFiled 也可以添加事件,这就是他的触发事件
-(void)textFileClick:(UITextField *)sender
{
_poiRequest.searchType = AMapSearchType_PlaceKeyword;
_poiRequest.keywords = sender.text;
_poiRequest.city = @[@"beijing"];
_poiRequest.requireExtension = YES;
[_search AMapPlaceSearch:_poiRequest];
}
#pragma mark-------- tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
AMapPOI *p=_dataArr[indexPath.row];
cell.textLabel.text=p.name;
return cell;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{//收起软键盘
[self.view endEditing:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end