Search code examples
swiftcoordinatesmapkitgeocodeclgeocoder

MapKit forward geocoding that is not based on ONLY addresses?


So I made a super simple forward geocoding function with MapKit:

func getCoordinates(from address: String) {
    CLGeocoder().geocodeAddressString(address) { placemark, error in
        guard error == nil && placemark != nil else {
            return
        }
        for index in 0..<placemark!.count {
            let number = index + 1
            print("Location \(number): \(placemark?[index].location?.coordinate)")
        }
    }
}

Now 2 things: The first is that (just to clarify), the mapkit geocode function only returns the top hit for the inputted string, it doesn't return an array of results--is there a way to change that? The second thing that I noticed is that this function doesn't work for 'Point of Interest' locations--for example, if you enter "McDonalds" as the string, you get no results back because it is not technically an address. Does anyone have any suggestions?


Solution

  • Try using MKLocalSearch instead:

    let request = MKLocalSearch.Request()
    request.region = region
    request.naturalLanguageQuery =  "McDonalds"
    let search = MKLocalSearch(request: request)
    search.start { (response, error) in
    
    }