2017-03-27 70 views
0

我試圖在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坊,這裏有我鍵入結果:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

爲什麼它返回Regina之外的位置,爲什麼在Rendek之前Rae St和Read Ave會出現?

+0

你嘗試過什麼至今:在指數路徑功能您細胞爲行,通過鍵入訪問數據?什麼不起作用或者提出了什麼錯誤? –

+1

只需構建您自己的'MKCoordinateRegion',例如'MKCoordinateRegionMakeWithDistance',並使用它。不需要使用地圖視圖。 – Rob

+0

另外,關於在全球範圍內獲得結果,您使用的是什麼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

回答

0

您將首先創建一個位置類來保持您的單元格組織。 (不要忘了導入MapKit這些視圖控制器)

import UIKit 
import MapKit 

class Location: NSObject { 
    var locationName: String! 
    var location: String! 

    init(locationName: String, location: String) { 
     self.locationName = locationName 
     self.location = location 
    } 
} 

然後,創建一個將存儲您TableViewController內部數據數組:

var locations = [Location]() 

您還需要在配置細胞功能的細胞類。

func configureLocationCell(locationName: String, location: String) { 
     self.myLocationNameLabel.text = locationName 
     self.myLocationLabel.text = location 
    } 

然後,執行MKLocalSearch請求,找到你的位置:(把這個文本框裏面的確發生了變化功能)

func performSearchRequest() { 
     let request = MKLocalSearchRequest() 
     let locationManager = CLLocationManager() 
     guard let coordinate = locationManager.location?.coordinate else { return } 

     request.naturalLanguageQuery = "\(myTextField.text)" 
     request.region = MKCoordinateRegionMakeWithDistance(coordinate, 3200, 3200) 
     // about a couple miles around you 

     MKLocalSearch(request: request).start { (response, error) in 
      guard error == nil else { return } 
      guard let response = response else { return } 
      guard response.mapItems.count > 0 else { return } 

      let randomIndex = Int(arc4random_uniform(UInt32(response.mapItems.count))) 
      let locationItems = response.mapItems[randomIndex] 

      for location in response.locationItems { 
       let location = Location(locationName: location.name!, location: "\(gyms.placemark.subLocality!)", "\(gyms.placemark.locality!)") 
       self.locations.append(location) 
       self.myTableView.reloadData() 
      } 
     } 
    } 

最後,在TableViewController 細胞數量功能, 回報locations.count

最後一兩件事:

let location = locations[indexPath.row] 
cell.configureLocationCell(locationName: location.locationName, location: location) 
相關問題