Search Nearby Locations Swift Example – MKLocalSearchRequest and UISearchController
Education swift

Search Nearby Locations Swift Example – MKLocalSearchRequest and UISearchController

Search Nearby Locations Swift Example – MKLocalSearchRequest and UISearchController

Search Nearby Locations swift – Apple maps introduced powerful native map API called MapKit. You can display a map, show the user’s current location, and drop annotation pins. And you don’t need to rely on any third-party SDKs.

Swift Example, building an app that shows nearby locations, shows geotagged data, or any operations that involves a map, MapKit is the tool for you.

A quick demo for the final project application so you can grasp some Idea of the end result.

 

 

In this application  UISearchController is used for searching any place or location on the map. UISearchController also provides realtime suggestions to the user. The search request is send to MapKit using MKLocalSearch function. MKLocalSearch requires a MKLocalSearchRequest and base upon the request the result can be handled through MKLocalSearchResponse.

In this project you are able to search for a location and based upon that location you can find nearby Restaurants, Lounge bar, Coffee shops. These request parameters are accepted by the MKLocalSearchApi.

I am going to show you the Syntax required for making the MKLocalSearchRequest and Syntax for MKLocalSearchResponse from the Api.

The Git link for the complete project can be find at the end of the post.

MKLocalSearch


Have you ever wondered which restaurants were nearby? Apple’s MapKit framework provides an easy way to lookup places via natural language search.
MkLocal Search API consist of two part first making the request to API and receiving the response from the server.

MKLocalSearchRequest


First we start with a simple search request. You will need to specify the text for the natural language search and an optional region in which the search will take place. For example, we will request the Api for providing results that comes inside the area of the map region from a MKMapView.

let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "Restaurants"
request.region = mapView.region

MKLocalSearchResponse


Next we make a MKLocalSearch from our request. We will get our search response from the completion block when calling the startWithCompletionHandler: method. In the block we get back an array of MKMapItem and an NSError. Each item in MKMapItem will be containing the response location properties like location name, contact details, web address, and physical address properties using CLPlacemark property.

let search = MKLocalSearch(request: request)
search.startWithCompletionHandler { response, error in
    guard let response = response else {
        print("There was an error searching for: \(request.naturalLanguageQuery) error: \(error)")
        return
    }

    for item in response.mapItems {
        // Display the received items
    }
}

 

Download complete project from git

IF you have any question or suggestion Please leave them in the comments and I will be happy to reply you back.

Thanks for the read.

Ashish Verma
Ashish is the chief editor at Technology and is also the founder of PocketCrunch. He believes if you are not updated, you are outdated. He likes to write about new technology trends and tutorials.
http://www.pocketcrunch.com

Leave a Reply