如何使用UISearchBar自动完成和建议搜索位置? [英] How to search for locations using UISearchBar with autocompletion and suggestions?

查看:191
本文介绍了如何使用UISearchBar自动完成和建议搜索位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,用户可以在其中搜索兴趣点,选择搜索结果,然后MKMapView将居中到结果坐标。

I am developing an app in which user can search for a point of interests, pick a search result and then the MKMapView will be centered to the result coordinate.

我的问题是如何使自动完成发生?我已经对 MKLocalSearch MKLocalSearchRequest 进行研究,似乎是苹果公司在iOS6.1上的位置搜索API +。但是,我找不到任何自动完成或建议与 MKLocalSearch MKLocalSearchRequest 的例子。是否可以自动完成位置搜索或显示建议列表,就像苹果的地图应用程序?谢谢!

My question is how to make autocompletion happen? I have did research on MKLocalSearch and MKLocalSearchRequest, and it seems that is Apple suggested API for location search on iOS6.1+. However I cannot find any examples with autocompletion or suggestions with MKLocalSearch and MKLocalSearchRequest. Is it possible to autocomplete a location search or display a list of suggestions just like Apple's Maps app? Thanks!

推荐答案

检查此帖子: http://stackoverflow.com/a/20141677/1464327

基本上,您可以发出多个请求。例如,当用户键入时,启动一个定时器,当定时器完成时,发出请求。

Basically, you can make multiple requests. For exemple, when the user types, start a timer, when the timer finishes, make a request. Whenever the user types, cancel the previous timer.

实现 textField:shouldChangeCharactersInRange:replacementString:

static NSTimer *_timer = nil;
[_timer invalidate];
_timer = [NSTimer timerWithTimeInterval:1.5 target:self selector:@selector(_search:) userInfo:nil repeats:NO];

然后实现_search方法来请求。

Then implement the _search method to make the request.

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = regionToSearchIn;
request.naturalLanguageQuery = self.textField.text;
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    // check for error and process the response
}];

我从来没有实现过这样的东西。我只是告诉我的起点是什么。希望这将给你一些方向。

I've never implemented something like this. I'm just telling what my starting point would be. Hopefully this will give you some direction.

这篇关于如何使用UISearchBar自动完成和建议搜索位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆