9.9. Searching on a Map View

Problem

You want to be able to provide search functionality to your users while they are viewing a map view. For instance, you may want to allow your users to search for all restaurants or gyms in a given region inside the map. So if the person is in the center of the town and she can see her location on the map, she can simply type “restaurants” in the search box and get your app to do the search for her.

Solution

Instantiate an object of type MKLocalSearchRequest and provide your search query, such as “restaurants,” for the request. Then you can submit your request to the iOS SDK using the MKLocalSearch class. The response that you get will be of type MKLocalSearchResponse.

Discussion

Map views are great, they really are. But what use do they have for the user if all she can see is just the map itself? The user may as well buy a traditional map on a piece of paper. Users like their smartphones’ map capabilities because they are interactive. The user can find things, search for locations, and get directions to an address. Apple has included three really handy classes in the iOS SDK that allow us to search for locations on the map. The searching is really easy. All you have to do is provide a text of what you are actually looking for, such as “restaurants” or “cafes,” and the SDK will do the rest of the job for you. For the purpose of this recipe, we are going to display a map view on our view controller, ask the map view to display the user’s location, and ...

Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.