2017-03-27 1 views
0

Swift 3.0에서 생성하려는 것은 미리 정의 된 범위 (예 : 100km)에 대해 현재 위치 (또는 지정된 위치)를 기반으로 한 간단한 위치 검색입니다. 나는 그 범위를 벗어나는 어떤 결과도 원하지 않습니다. 나는지도를 보거나 그것에 핀을 붙이고 싶지 않습니다. 검색을 원하고 그 결과를 돌려 주면됩니다. 지도 키트로 가능합니까? 내가 찾은 모든 예제 또는 튜토리얼은 맵으로 시작하여 맵을 사용하여 region/span을 가져온 다음 결과가있는 맵으로 리턴합니다. 이 예제 중 일부를 시도해 보면지도 영역에만 국한되지 않고 다른 국가에서도 검색 결과를 얻을 수 있습니다. 내가 원하는 것은 선택된 위치를 반환하는 단일 화면 검색입니다. 지도가 없습니다. 세계의 다른 쪽에서 결과가 없습니다. mapkit이 아닌 다른 것을 사용해야합니까? 아니면 mapview없이이 작업을 수행 할 수 있습니까?지도를 사용하지 않고 Swift에서 위치 검색을 수행 할 수 있습니까?

편집 : @ 롭에서 제공하는 정보와 , 나는

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() 
     } 
    } 
} 

그러나 나는 아직도 조금 혼란 스러워요 ... 여기까지입니다. 그것은 여전히 ​​모든 곳에서 결과를 가져 오는 것 같습니다.

enter image description here

enter image description here

enter image description here

enter image description here : 나는 레지나, SK의 위치에 따라 주소 19 Rendek 초승달을 찾고 있었다 예를 들어, 여기에 내가 입력 할 때 결과는

enter image description here

Rendek이 아닌 다른 지역으로 돌아 오는 이유는 무엇이며 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

셀을 구성한 상태로 유지하기 위해 위치 클래스를 만들어야합니다. 당신의 TableViewController 내부에 데이터를 저장할 배열을 만든 다음

import UIKit 
import MapKit 

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

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

을 (이보기 컨트롤러에 MapKit을 가져올 것을 잊지 마십시오) : 또한의 구성 세포의 기능을 필요로

var locations = [Location]() 

당신의 세포 계급.

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

다음, 당신의 위치를 ​​찾기 위해 MKLocalSearch 요청을 수행한다 : (a 텍스트 필드 안에이를 넣어 한 변경 기능)

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) 
관련 문제