您知道 Apple 官方提供了一个免费限制的地点搜索 API MKLocalSearch 吗?MWPlaceSearch 是一个用 Swift 编写的易于使用的 MKLocalSearch 库。
import UIKit
import MapKit
import MWPlaceSearch
class ViewController: UIViewController, MWPlaceSearchDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Find a sushi place in Vancouver
// You can provide a region as a hint. It can also be nil.
let coordinate = CLLocationCoordinate2DMake(49.282909, -123.1273396) // Vancouver
let region = MKCoordinateRegionMakeWithDistance(coordinate, 1000.0, 1000.0) // 1km * 1km
MWPlaceSearch(delegate: self).search("Sushi Vancouver", withRegion: region)
}
// This function will be called when a search had done or failed.
func didFinishSearchingPlace(result: MWPlaceSearchResult) {
switch result.response {
case .Success:
guard let data = result.data else { return }
data.forEach {
if let name = $0.name {
print("name => \(name)")
print("coordinate => \($0.placemark.coordinate.latitude), \($0.placemark.coordinate.longitude)")
print("address => \($0.placemark.address)")
}
}
case .Failure:
()
}
}
}
pod "MWPlaceSearch"
Watanabe Masahiro,[电子邮箱保护]
MWPlaceSearch 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。