iosswiftmkmapviewmklocalsearch

How to use MKLocalSearch In MKMapView


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


Solution

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