我試圖在Swift 3.0中創建的是基於當前位置(或指定位置)的預定義範圍(例如100公里)的簡單位置搜索。我不希望任何超出該範圍的結果。我不想看到地圖,把任何引腳,等等。只是想要搜索,然後返回結果。這可能與mapkit?我找到的每個示例或教程都從地圖開始,然後使用地圖獲取地區/跨度,然後返回地圖並顯示結果。在嘗試其中一些例子時,搜索並不侷限於地圖區域,因爲我在其他國家獲得了結果。我想要的是一個單一的屏幕搜索,只是返回一個選定的位置。沒有地圖。沒有來自世界另一端的結果。我應該使用mapkit以外的東西嗎?還是我可以在沒有mapview的情況下完成這項工作?我可以在不使用地圖的情況下在Swift中進行位置搜索嗎?
編輯: 與@Rob提供的信息,我到這裏...
extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
var localRegion: MKCoordinateRegion
let distance: CLLocationDistance = 1200
let currentLocation = CLLocationCoordinate2D.init(latitude: 50.412165, longitude: -104.66087)
localRegion = MKCoordinateRegionMakeWithDistance(currentLocation, distance, distance)
print(localRegion)
guard let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = localRegion
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}
不過,我還是有點糊塗。它似乎仍然從全面提出結果。例如,如果我是根據在里賈納,SK的位置尋找地址19 Rendek坊,這裏有我鍵入結果:
爲什麼它返回Regina之外的位置,爲什麼在Rendek之前Rae St和Read Ave會出現?
你嘗試過什麼至今:在指數路徑功能您細胞爲行,通過鍵入訪問數據?什麼不起作用或者提出了什麼錯誤? –
只需構建您自己的'MKCoordinateRegion',例如'MKCoordinateRegionMakeWithDistance',並使用它。不需要使用地圖視圖。 – Rob
另外,關於在全球範圍內獲得結果,您使用的是什麼API? ['geocodeAddressString'](https://developer.apple.com/reference/corelocation/clgeocoder/1423509-geocodeaddressstring)將在任何地方查找地址。但['MKLocalSearch'](https://developer.apple.com/reference/mapkit/mklocalsearch)將使用['region'](https://developer.apple.com/reference/mapkit/mklocalsearchrequest/1451919-區域)提供給['MKLocalSearchRequest'](https://developer.apple.com/reference/mapkit/mklocalsearchrequest)。 – Rob