SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

How to look up a location with MKLocalSearch.Request

Swift version: 5.1

Paul Hudson    @twostraws   

MapKit has built-in functionality to let us look up places and businesses around the world, all using natural language searches that can be passed in straight from user entry.

First, import the MapKit framework, then create an instance of MKLocalSearch.Request that contains what you want to search for.

For example, this looks for Fortnum and Mason in London:

let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = "Fortnum and Mason, London"

That provides no other information to Apple other than the text string, so it will look everywhere in the world for such a match.

If you wanted, you could provide a specific search region by letting the user pan around an MKMapView for a specific location, then passing the region they are looking at to your search:

searchRequest.region = yourMapView.region

Once you’re ready, wrap the request inside an instance of MKLocalSearch, like this:

let search = MKLocalSearch(request: searchRequest)

When you’re ready, call the start() method on your search. This accepts one parameter, which is a closure that runs when the search completes – it will be handed the response data or an error, depending on what happened. This closure will always be run on your application’s main thread, so you can present some user interface immediately if you want.

As an example, this code will loop over all the results that were found for the search, printing out the phone number for each one:

search.start { response, error in
    guard let response = response else {
        print("Error: \(error?.localizedDescription ?? "Unknown error").")

    for item in response.mapItems {
        print(item.phoneNumber ?? "No phone number.")

Even without a map region, Apple Maps found the London store just fine, but obviously passing in a map region will help your accuracy improve.

If for some reason the request didn’t return quickly enough and you no longer need a response, call its cancel() method to abort the request.

SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Save 50% on all books and bundles

The biggest ever Hacking with Swift sale is now on, letting you save 50% on all books and bundles. Learn something new with Swift and enjoy great savings while the sale lasts!

Click here to save 50% in our Black Friday sale!

Available from iOS 6.1

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5