0

我在 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 = ""; mapItems = (" {\n isCurrentLocation = 0;\n name = India;\n placemark = \"India, India @ <+23.04117260,+78.89180550> +/- 0.00m, region CLCircularRegion (标识符:'<+21.84329084, +82.78786665> 半径 2237301.34',中心:<+21.84329084,+82.78786665>,半径:2237301.34m)\";\n}" ); }

4

1 回答 1

0

一切正常..只是您在不包含 naturalLanguageQuery 的请求中犯了一个错误

 let request = MKLocalSearchRequest()
 request.region = mapView.region
 let search = MKLocalSearch(request: request)

在这种情况下request.naturalLanguageQuery,将其设置为 nil ......所以你得到了未知类型的(MKErrorDomain 错误 1)。看看这里naturalLanguageQuery的 Apple 文档不能包含 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)
于 2015-05-21T12:56:39.607 回答