-
-
Save engin7/faced0f97f8ce2b3d951d11b2812abcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import MapKit | |
final class MapKitService { | |
// Map the Apple Category to your own category | |
private let typesToDrink: [MKPointOfInterestCategory] = [.brewery, .cafe, .winery] | |
private let typesToEat: [MKPointOfInterestCategory] = [.foodMarket, .restaurant] | |
func retrieve(from: String, completionBlock: @escaping ([Place]) -> Void) { | |
var places = [Place]() | |
// Create the request | |
let request = MKLocalSearch.Request() | |
request.naturalLanguageQuery = from | |
// Define what you want in the result | |
request.resultTypes.insert(.address) | |
request.resultTypes.insert(.pointOfInterest) | |
// Add a filter on the Category of the place | |
var allTypes = typesToDrink | |
allTypes.append(contentsOf: typesToEat) | |
let filter = MKPointOfInterestFilter(including: allTypes) | |
request.pointOfInterestFilter = filter | |
// Create the search | |
let search = MKLocalSearch(request: request) | |
// Launch the request | |
search.start { response, error in | |
guard let response = response else { | |
if let error = error { | |
print("\(error.localizedDescription)") | |
} | |
return | |
} | |
//Answers | |
for item in response.mapItems { | |
if let name = item.name { | |
var place = Place() | |
place.name = name | |
//Webiste | |
if let website = item.url { | |
place.url = website.absoluteString | |
} | |
//Address | |
var address = Address() | |
address.street = item.placemark.thoroughfare.orEmpty | |
// ... Handle the rest of the address | |
place.addresses.append(address) | |
places.append(place) | |
} | |
} | |
completionBlock(places) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment