Apologies
You must be signed in to watch this lesson.
Find Places with MKLocalSearch
Maps UIKit SwiftUI
With Apple's MapKit framework, they make it very easy to search for local places using the MKLocalSearch api. In order to initiate a search, we simple need to pass it a Request object that is configured with a search query and a region. In this lesson, I'll go over exactly how to perform search as well as placing these items onto the map.

Comments (9)
fkeebie
4 years ago
I don't know whether this is a problem or not. I myself am living in Tokyo and MKLocalSearch returns mapItems in Tokyo, say Apple Shinjuku, Apple Omotesando, Apple Ginza and so on, on a live preview as well as on a real iOS device. I also ran the code I downloaded from your server and I got the same result. Irrespective of the region set by the code, MKLocalSearch seems to search places where my Mac or iOS device actually exists, in my case, it is Tokyo. After having googled, I finally got the same result as you explained in your lesson if I set the region again by repeating the following code instead of "request.region = mapView.region", let centerCoordinate = CLLocationCoordinate2D(latitude: 37.76666, longitude: -122.427290) let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1) let region = MKCoordinateRegion(center: centerCoordinate, span: span) request.region = region Anyway, I'm enjoying this new course!!
Brian Voong
4 years ago
fkeebie
4 years ago
Thank you for your reply, Brian.
albaqawi
4 years ago
Hi Brain, I love the class, but every time I run the code even you ideal code from server I get the following address "Apple Valley, MN United States" I do not get like you Apple stores in the region of SF.... is htis related to setting the region to local area of location where device is located? and is this covered in future?
fkeebie
4 years ago
To avoid the repetition of the code, I added a return value like " fileprivate func setupRegionForMap() -> MKCoordinateRegion{ .... return region}".
rlew23
4 years ago
Enjoying your course so far Brian. Thank you. I found by accident "placemark.title" renders a properly formatted address already...any reasons not to use that if it fits address requirements?
Brian Voong
4 years ago
NSD_bigjas00
4 years ago
Another great course!! I refactored the code to get the address string with the following function: fileprivate func displayAddress(from placemark: MKPlacemark) -> String { var addressString = "" if let number = placemark.subThoroughfare, let street = placemark.thoroughfare, let zip = placemark.postalCode, let city = placemark.locality, let state = placemark.administrativeArea { addressString = "\(number) \(street),\(city),\(state) \(zip)" } return addressString } Usage: let address = self.displayAddress(from: mapItem.placemark) print(address)
DataHog
4 years ago
I am using Xcode 11.3.1 and having issues with MkLocalSearch localsearch.start changing my region to my local region. prior to this: let localSearch = MKLocalSearch(request: request) my mapview.region is: ▿ MKCoordinateRegion ▿ center : CLLocationCoordinate2D - latitude : 37.334599999999995 - longitude : -122.00919999999999 ▿ span : MKCoordinateSpan - latitudeDelta : 90.0 - longitudeDelta : 180.0 after executing localSearch.start { (resp, err) and within the mapItems.forEach I get this: 2020-01-30 15:37:35.009308-0500 MapDirectionsGooglePlaces_LBTA[18329:1683505] libMobileGestalt MobileGestalt.c:1647: Could not retrieve region info and po resp ▿ Optional<MKLocalSearchResponse> - some : <MKLocalSearchResponse: 0x600000a98000> { boundingRegion = "<center:+34.01132621, -84.27239180 span:+0.33391947, +0.58232912>"; mapItems = ( "<MKMapItem: 0x600003081f80> {\n isCurrentLocation = 0;\n name = \"Apple Avalon\";\n phoneNumber = \"\\U202d+1 (770) 510-1670\\U202c\";\n placemark = \"Apple Avalon, 8130 Avalon Blvd, Alpharetta, GA 30009, United States @ ... I've spend quite a bit of time searching for a reason without finding anything. Thanks
Brian Voong
4 years ago
DataHog
4 years ago
Thanks for the reply. yes, per the tutorial the mapview region is Seattle at - latitude : 37.334599999999995, - longitude : -122.00919999999999 which is shown above. within localSearch.start it has changed to the Atlanta area (I live in Acworth, GA) with: boundingRegion = "<center:+34.01132621, -84.27239180 span:+0.33391947, +0.58232912>"; see above. Also note this from above note: after executing localSearch.start { (resp, err) and within the mapItems.forEach I get this: 2020-01-30 15:37:35.009308-0500 MapDirectionsGooglePlaces_LBTA[18329:1683505] libMobileGestalt MobileGestalt.c:1647: Could not retrieve region info
jademagnum
4 years ago
Hey Brian, In my project when search for "airport", I get airports in Tampa even tho my region is set to SF. I downloaded the project file and ran your project and I am getting the same results. I can't figure out why it isn't searching for airports in SF.
Brian Voong
4 years ago
jademagnum
4 years ago
Awesome, Thanks for the quick response!
Setwork44
4 years ago
Hey Everyone. Ok, so if I wanted to add a fixed array of Strings that I wanted the users to search how can I do that? Like if I wanted to them to only be able to search for a fix array, like [doctor, nurse, clearner, admin] and only these terms... Does anyone know if that's possible?
Brian Voong
4 years ago
Setwork44
4 years ago
Thanks so much for helping with this! I have about 20-30 job title strings to set, Could I use a picker view instead to search? What’s your thoughts on the most user friendly way to approach this and have all the “nurses” for example then show up on the map?
Brian Voong
4 years ago
Setwork44
4 years ago
That’s exquisite! Thank you so much for your inspiration man :)
Setwork44
4 years ago
That ALONE was worth the money I paid for this module!!
mcvrishdc
3 years ago
Brian: This may be a variation on questions already answered, but when I run the app, the debug panel shows the following: 2021-05-04 07:13:59.647223-0400 maps[62817:3466197] Metal API Validation Enabled 2021-05-04 07:13:59.851746-0400 maps[62817:3466197] libMobileGestalt MobileGestalt.c:1647: Could not retrieve region info Apple World Trade Center Apple West 14th Street Apple SoHo Apple Short Hills Apple Upper West Side Apple Grand Central Apple Staten Island Apple Fifth Avenue Apple Downtown Brooklyn Apple Upper East Side The pins cannot be placed because the query is yielding NY rather than SF stores. How do I change that?
mcvrishdc
3 years ago
To follow up on the previous post, I checked the code against the reference code. It is now identical. Now when the app is tested, the area shown flios between the New York City area bd the Philadelphia area where I live.
HELP & SUPPORT