How to use MKLocalSearch In MKMapView

会有一股神秘感。 提交于 2019-12-12 00:15:46

问题


i am using MKLocalSearch in MKMapView..I am implementing as follow

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)")

            }
        })



    }


}

Output: Google VC coordinate is as 72.8561644 19.0176147 Error occured in search: The operation couldn’t be completed. (MKErrorDomain error 1.) Why is this happening?

EDITED: however when i change request as

let request = MKLocalSearchRequest()
 request.naturalLanguageQuery = "india"
 //request.region = mapView.region
 let search = MKLocalSearch(request: request)

I get the good response as

{ boundingRegion = ""; mapItems = ( " {\n isCurrentLocation = 0;\n name = India;\n placemark = \"India, India @ <+23.04117260,+78.89180550> +/- 0.00m, region CLCircularRegion (identifier:'<+21.84329084,+82.78786665> radius 2237301.34', center:<+21.84329084,+82.78786665>, radius:2237301.34m)\";\n}" ); }


回答1:


Everything is working fine..Just you did a mistake on request that doesnot contains a naturalLanguageQuery as

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

In this case request.naturalLanguageQuery is set it to nil....so you got (MKErrorDomain error 1) of unknown type.Look on Apple docs herenaturalLanguageQuery cannot contain nil value...So make a request with it

let request = MKLocalSearchRequest()
 request.naturalLanguageQuery = "india"
 let search = MKLocalSearch(request: request)

You can use region parameter to narrow the list of search results to those inside or close to the specified region. Specifying a region does not guarantee that the results will all be inside the region. It is merely a hint to the search engine. So region can act as an optional here. Or you can make request as for better results as

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


来源:https://stackoverflow.com/questions/30366060/how-to-use-mklocalsearch-in-mkmapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!