我posted this issue on GitHub,虽然它已经过了一个星期而开发人员没有回复,所以希望能在这里得到答案。
使用示例代码,再加上一点来显示从ForwardGeocodeOptions返回的地标,我想出了这个测试代码:
(Swift 3,Xcode 8)
func mapView(_ mapView: MGLMapView, regionDidChangeAnimated animated: Bool) {
geocodingDataTask?.cancel()
self.outputText.text = ""
// Variables.userLat and Variables.userLng are set through locationManager
let options = ReverseGeocodeOptions(coordinate: CLLocationCoordinate2D(latitude: Variables.userLat, longitude: Variables.userLng))
geocodingDataTask = geocoder.geocode(options) { [unowned self] (placemarks, attribution, error) in
if let error = error {
NSLog("%@", error)
} else if let placemarks = placemarks, !placemarks.isEmpty {
self.resultsLabel.text = placemarks[0].qualifiedName
let foptions = ForwardGeocodeOptions(query: self.inputText.text!)
// To refine the search, you can set various properties on the options object.
foptions.allowedISOCountryCodes = ["US"]
foptions.focalLocation = CLLocation(latitude: Variables.userLat, longitude: Variables.userLng)
let neLat = Variables.userLat + 1.0
let neLng = Variables.userLng + 1.0
foptions.allowedRegion?.northEast = CLLocationCoordinate2D(latitude: neLat, longitude: neLng)
let swLat = Variables.userLat - 1.0
let swLng = Variables.userLng - 1.0
foptions.allowedRegion?.southWest = CLLocationCoordinate2D(latitude: swLat, longitude: swLng)
foptions.allowedScopes = [.address, .pointOfInterest]
let _ = geocoder.geocode(foptions) { (placemarks, attribution, error) in
guard let placemark = placemarks?.first else {
return
}
let coordinate = placemark.location.coordinate
print("\(coordinate.latitude), \(coordinate.longitude)")
self.inputLat.text = coordinate.latitude.description
self.inputLng.text = coordinate.longitude.description
var string = ""
for mark in placemarks! {
if string != "" {
string += "\n"
}
string += mark.qualifiedName
}
self.outputText.text = string
}
} else {
self.resultsLabel.text = "No results"
}
}
}
这给了我一个迷你应用程序来测试我在Xcode Simulator中更改位置时返回的数据。 截图2017-07-12 13 54 09
正如你从这张照片中看到的那样,我把地图集中在詹克斯,OK(塔尔萨以外的一个小镇,OK - 有点像'美国中部'的位置。)
当在该地区寻找一个共同的地方时(“沃尔玛” - 位于阿肯色州附近,所以周围有很多),你可以看到只有2个“本地”沃尔玛回来搜索。 / p>
现在,让我们搬到AR的本顿维尔 - 沃尔玛的故乡......
并且,我们得到两个最新的结果,但其他的是相同的(比塔尔萨更远,好的.....)
我们发现,如果我们将城镇添加到搜索的第一个区域,结果会更好:
(我们所做的每一次搜索都有类似的结果 - 美国各地的城市以及Quiznos等其他“常见地点”(类似于沃尔玛在他们的家乡丹佛,科罗拉多州的结果......)
从我的代码中可以看出,我尝试使用allowedRegion?.northEast和southWest(根据我的理解,那些应该将搜索区域设置为位置周围约100英里,但我不确定我是否设置了右边),虽然没有发现这个设置的差异(即,有/没有我得到相同的结果)。
唯一“更好”的结果是将城镇名称与“普通”名称一起放入(但奇怪的是,如果城镇名称在普通名称之前或之后,则会返回不同的结果 - 我没有确切地检查虽然我认为他们是“最好的”(即地点都非常接近),而不是将城镇名称放在普通的名字之后)
如果不告诉用户输入城镇名称(不是一个非常理想的计划,我该怎么做才能获得更好的结果!)
提前感谢您提示 - 查找是应用程序的关键部分(不是图片中显示的测试内容!),用户希望拉出几个“附近”的公共场所(在这种情况下,我们会期待在20英里范围内看到所有5个结果 - 当然不超过100英里),所以对我们来说这个工作比我们现在看到的更可靠,这对我们来说非常重要。