nickcoding
nickcoding

Reputation: 475

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?

Upvotes: 0

Views: 123

Answers (1)

Gerd Castan
Gerd Castan

Reputation: 6849

Try using MKLocalSearch instead:

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

}

Upvotes: 1

Related Questions