我在MKMapView中使用MKLocalSearch ..我正在实施如下
extension MYClass: SendLocationDelegate{
func sendCoOrdinates(loccoordinate:CLLocation, placemark:CLPlacemark){
println(" Google VC coordinate is as \(loccoordinate.coordinate.longitude) \(loccoordinate.coordinate.latitude)")
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude:loccoordinate.coordinate.latitude, longitude: loccoordinate.coordinate.longitude)
let theSpan : MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta:0.01)
let theRegion : MKCoordinateRegion = MKCoordinateRegion(center: location, span: theSpan)
self.mapView.setRegion(theRegion, animated: false)
let request = MKLocalSearchRequest()
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({(response: MKLocalSearchResponse!,
error: NSError!) in
if error != nil {
println("Error occured in search: \(error.localizedDescription)")
} else if response.mapItems.count == 0 {
println("No matches found")
} else {
println("Matches found")
println("\(response)")
}
})
}
}
输出:Google VC坐标为72.8561644 19.0176147 搜索中出错:无法完成操作。 (MKErrorDomain错误1。)为什么会发生这种情况?
已编辑:但是当我将请求更改为
时let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "india"
//request.region = mapView.region
let search = MKLocalSearch(request: request)
我得到了良好的回应
{ boundingRegion ="&#34 ;; mapItems =( " {\ n isCurrentLocation = 0; \ n name = India; \ n placemark = \" India,India @ 1 + 23.04117260,+ 78.89180550> +/- 0.00m,区域CLCircularRegion (标识符:'< + 21.84329084,+ 82.78786665> radius 2237301.34', center:< + 21.84329084,+ 82.78786665&gt ;, radius:2237301.34m)\&#34 ;; \ n}" ); }
答案 0 :(得分:0)
一切都工作得很好。只是你在没有包含naturalLanguageQuery的请求时犯了一个错误
let request = MKLocalSearchRequest()
request.region = mapView.region
let search = MKLocalSearch(request: request)
在这种情况下,request.naturalLanguageQuery
被设置为nil ....所以你得到(MKErrorDomain错误1)未知类型。查看Apple文档here naturalLanguageQuery
不能包含nil值...所以用它来提出请求
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "india"
let search = MKLocalSearch(request: request)
您可以使用region
参数将搜索结果列表缩小到指定区域内或附近的搜索结果列表。指定区域并不能保证结果全部都在区域内。这只是搜索引擎的一个提示。所以region
可以在这里作为选项。或者你可以提出更好的结果请求
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "india"
request.region = mapView.region
let search = MKLocalSearch(request: request)