如何使用Apple Map Kit实现地址的自动完成 [英] How to implement auto-complete for address using Apple Map Kit

查看:221
本文介绍了如何使用Apple Map Kit实现地址的自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动填写用户的地址,与google api在此链接中提供的地址相同:

I want to auto-complete the address for the user as same as what google api provides in this link:

https://developers.google.com/maps/documentation/javascript/places-autocomplete?hl=en <

如何使用Apple地图套件实现相同的功能?

How can i implement the same functionality using apple map kit?

我试过使用Geo Coder,我写了这个例子:

I have tried to use the Geo Coder, i wrote this for example:

@IBAction func SubmitGeoCode(sender: AnyObject) {

    let address = "1 Mart"
    let coder = CLGeocoder()

    coder.geocodeAddressString(address) { (placemarks, error) -> Void in

        for placemark in placemarks! {

            let lines = placemark.addressDictionary?["FormattedAddressLines"] as? [String]

            for addressline in lines! {
                print(addressline)
            }
        }
    }
}

然而,结果非常令人失望。

However the results are very disappointing.

任何可用于实现此类功能的Apple API,还是应该使用google api?

Any Apple APIs available to implement such functionality, or should i head for google api ?

谢谢

推荐答案

更新 - 我创建了一个简单的使用Swift 3作为原始答案的示例项目此处是用Swift 2编写的。

Update - I've created a simple example project here using Swift 3 as the original answer was written in Swift 2.

在iOS 9.3中引入了一个名为 MKLocalSearchCompleter 的新类,这允许创建自动完成解决方案,您只需传入queryFragment如下:

In iOS 9.3 a new class called MKLocalSearchCompleter was introduced, this allows the creation of an autocomplete solution, you simply pass in the queryFragment as below:

var searchCompleter = MKLocalSearchCompleter()
searchCompleter.delegate = self
var searchResults = [MKLocalSearchCompletion]()

searchCompleter.queryFragment = searchField.text!

然后使用 MKLocalSearchCompleterDelegate

extension SearchViewController: MKLocalSearchCompleterDelegate {

    func completerDidUpdateResults(completer: MKLocalSearchCompleter) {
        searchResults = completer.results
        searchResultsTableView.reloadData()
    } 

    func completer(completer: MKLocalSearchCompleter, didFailWithError error: NSError) {
        // handle error
    }
}

并以适当的格式显示地址结果:

And display the address results in an appropriate format:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let searchResult = searchResults[indexPath.row]
    let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: nil)
    cell.textLabel?.text = searchResult.title
    cell.detailTextLabel?.text = searchResult.subtitle
    return cell
}

然后,您可以使用 MKLocalCompletion 对象来实例化 MKLocalSearchRequest ,从而获得对的访问权限MKPlacemark 和所有其他有用的数据:

You can then use a MKLocalCompletion object to instantiate a MKLocalSearchRequest, thus gaining access to the MKPlacemark and all other useful data:

let searchRequest = MKLocalSearchRequest(completion: completion!)
let search = MKLocalSearch(request: searchRequest)
search.startWithCompletionHandler { (response, error) in
    let coordinate = response?.mapItems[0].placemark.coordinate
}

这篇关于如何使用Apple Map Kit实现地址的自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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