【问题标题】:MKLocalSearchRequest giving me weird resultsMKLocalSearch 请求给了我奇怪的结果
【发布时间】:2016-02-06 16:07:08
【问题描述】:

所以我为我的地图应用程序实现了searchBar,它给了我一些奇怪的结果。例如,如果我搜索“Oslo”,它会将我指向城市北部一个非常具体但奇怪的地方,然后放大得很远。这里的代码一定是我做错了。任何人都能够发现这个问题?

// When user submits search query
func searchBarSearchButtonClicked(searchBar: UISearchBar){
    // resign first responder and dismiss the searchbar.
    searchBar.resignFirstResponder()
    dismissViewControllerAnimated(true, completion: nil)

    // Create and start search request
    localSearchRequest = MKLocalSearchRequest()
    localSearchRequest.naturalLanguageQuery = searchBar.text
    localSearch = MKLocalSearch(request: localSearchRequest)

    localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

        if localSearchResponse == nil{
            let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alertController, animated: true, completion: nil)
            return
        }

        let center = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)
        self.updateCurrentMapRegion(center, distance: 5000)
        NSNotificationCenter.defaultCenter().postNotificationName("LocationUpdate", object: nil, userInfo: ["latitude": center.latitude, "longitude": center.longitude])
    }
}

另外,一个问题是我在获得搜索结果后更新地图区域时将距离硬编码为 5000 米。我怎样才能以更动态的方式处理这个问题?

什么是使用 MKLocalSearchRequest() 的替代方法,在我看来,这听起来像是你在寻找附近的东西而不是更大的实体(如城市等)时应该使用的东西。

【问题讨论】:

    标签: swift mapkit


    【解决方案1】:

    第一件事:MKLocalSearch 是您要使用的正确 API,尽管您正在做的事情听起来有点奇怪!

    MKLocalSearch 会为您提供位置列表,然后返回位置周围的边界框,以最适合它认为您正在寻找的内容。我尝试运行您的代码并为奥斯陆找到了这个:

    您遇到的问题是您使用边界框的中心作为大头针位置,而不是奥斯陆地标。

    所以这里不是你的代码行:

    let center = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude,
        longitude: localSearchResponse!.boundingRegion.center.longitude)
    

    你会使用:

    let center = localSearchResponse?.mapItems.first?.placemark.coordinate
    

    (这是一个可选的,但你可以选择如何处理它。稍后我会告诉你我喜欢的方式。)

    此外,这解决了您关于放大多远的问题。很简单,您可以使用区域的边界框作为缩放级别。我不确定你的 updateCurrentMapRegion:distance 方法是如何工作的,但是 MKMapView 有你可以使用的方便的 setRegion:animated 方法(如果你有访问权限的话)。

    mapView.setRegion(localSearchResponse!.boundingRegion, animated: true)
    

    总结一下,如果你有 MapView 的引用,你应该可以用这个替换你的完成块并让它工作:

    guard let localSearchResponse = localSearchResponse, mapItem = localSearchResponse.mapItems.first else {
        let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        return
    }
    
    let center = mapItem.placemark.coordinate
    self.mapView.setRegion(localSearchResponse.boundingRegion, animated: true)
    NSNotificationCenter.defaultCenter().postNotificationName("LocationUpdate", object: nil, userInfo: ["latitude": center.latitude, "longitude": center.longitude])
    

    在这个块中,我还用guard 替换了您的if 语句来设置这两个值,因此我们不再需要强制解包选项。如果您无法在此方法中访问mapView,您可以切换updateCurrentMapRegion:center 使其更适合,或者进行一些数学运算以使边界区域适合这些值。

    【讨论】:

      【解决方案2】:

      您不应该使用 MKLocalSearchRequest() 而是使用 MKLocalSearchCompleter,它可以提供更好的结果并在当前的 Apple 地图中使用。

      您可以在
      answer

      中了解如何实现此功能

      【讨论】:

      • 您能解释一下结果如何更好吗?
      • 它们不是随机的。在使用 MKLocalSearchRequest 时,您经常会得到像世界另一端的国家这样的结果,同时却没有得到距离您 100 米的街道。使用 MKLocalSearchCompleter 你会得到更合理的结果。
      猜你喜欢
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2013-05-29
      • 2016-12-13
      • 2018-03-13
      相关资源
      最近更新 更多