码迷,mamicode.com
首页 > 其他好文 > 详细

高德地图搜索 用tablevie列表显示

时间:2015-06-04 18:43:46      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

#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)];

    _textFile.placeholder=@"请输入搜索地址";

    _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

 

高德地图搜索 用tablevie列表显示

标签:

原文地址:http://www.cnblogs.com/huketianxia/p/4552319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!