---
title: "#47: MKLocalSearch 📍"
url: https://littlebitesofcocoa.com/47-mklocalsearch
author: Jake Marsh
published: 2015-07-28
topics: ["MapKit","Core Location"]
description: "MKLocalSearch is a wonderful little gem hidden inside MapKit. It allows an app to perform a natural language query for local points of interest near a given..."
---

# #47: MKLocalSearch 📍

By Jake Marsh, July 28, 2015. HTML: https://littlebitesofcocoa.com/47-mklocalsearch

**MKLocalSearch** is a wonderful little gem hidden inside **MapKit**. It allows an app to perform a natural language query for local points of interest near a given latitude and longitude. Today we'll use it to build a simple app to find us a place to eat lunch.

``` swift
manager = CLLocationManager()
manager!.delegate = self
manager!.desiredAccuracy = kCLLocationAccuracyThreeKilometers
manager!.requestWhenInUseAuthorization()
manager!.requestLocation()

// then later, once we have a location:

let request = MKLocalSearchRequest()

request.naturalLanguageQuery = "Restaurants"
request.region = MKCoordinateRegionMakeWithDistance(location.coordinate, 1600, 1600)

MKLocalSearch(request: request).startWithCompletionHandler { (response, error) in
  guard error == nil else { return }
  guard let response = response else { return }
  guard response.mapItems.count > 0 else { return }

  let randomIndex = Int(arc4random_uniform(UInt32(response.mapItems.count)))
  let mapItem = response.mapItems[randomIndex]

  mapItem.openInMapsWithLaunchOptions(nil)
}
```

First we set up our `CLLocationManager`, and use the new **iOS 9** method for requesting a “single location” instead of continuous updates.

Then we create the `MKLocalSearchRequest` object and tell it to search **"Restaurants"** within 1600 meters (about a mile) of the user's current location.

After that it's just a matter of waiting for results, choosing one at random, and then we cheat a bit (since this is just a demo) and pop the user out to the **Maps app** to view the chosen result.

**Download the example project here: [j.mp/bite047](http://j.mp/bite047)**
