简体   繁体   English

MKLocalSearchCompleter - 忽略区域

[英]MKLocalSearchCompleter - Ignores region

I'm trying to make an address search using the MKLocalSearchCompleter .我正在尝试使用MKLocalSearchCompleter进行地址搜索。 I've provided it with a region(Australia) but the provided region is completely ignored, I still get results from around the world.我已经为它提供了一个地区(澳大利亚),但提供的地区被完全忽略了,我仍然得到来自世界各地的结果。

...

private var searchCompleter = MKLocalSearchCompleter()

....

init {
    searchCompleter.region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -25.2744, longitude: 133.7751), span: MKCoordinateSpan(latitudeDelta: 40, longitudeDelta: 40)) // Australia
    searchCompleter.resultTypes = [.address]
    searchCompleter.delegate = self

    searchCompleter.queryFragment = "Wycombe Road"
}

extension MapSearchCompleter: MKLocalSearchCompleterDelegate {
    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        print("+++ \(completer.results)")
    }
}

If I try to print the region in the delegate method completerDidUpdateResults , I get the region back.如果我尝试在委托方法completerDidUpdateResults中打印该区域,我会取回该区域。

+++ MKCoordinateRegion(center: __C.CLLocationCoordinate2D(latitude: -25.2744, longitude: 133.7751), span: __C.MKCoordinateSpan(latitudeDelta: 40.0, longitudeDelta: 40.0)) +++ MKCoordinateRegion(中心:__C.CLLocationCoordinate2D(纬度:-25.2744,经度:133.7751),跨度:__C.MKCoordinateSpan(latitudeDelta:40.0,longitudeDelta:40.0))

Any idea how to make it work?知道如何使它工作吗? Not sure what I'm doing wrong here.不知道我在这里做错了什么。

I can just filter out addresses manually, but I don't think that the right way to do it.我可以手动过滤掉地址,但我认为这不是正确的方法。

You can try this way `你可以试试这个方法`

        let request = MKLocalSearch.Request()
        request.naturalLanguageQuery = searchTxt
        request.region = mkMapView.region
        // Fetch
        MKLocalSearch(request: request).start { (response, _) in
            guard let result = response else { return }
            self.place = result.mapItems.compactMap({ (item) -> Place? in
                return Place(placemark: item.placemark)
            })
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM