Ron Tabachnik
Ron Tabachnik

Reputation: 5

MKLocalSearchCompleter result in English

I am implementing an auto-complete feature for users to search for address based on their input. However, I want to ensure that the search results are always in English, regardless of the user's phone language. While I considered using completer.region of English-speaking countries, this would restrict the feature's usability and provide me results only from the region I specified.

private lazy var localSearchCompleter: MKLocalSearchCompleter = {
        let completer = MKLocalSearchCompleter()
        completer.delegate = self
        completer.resultTypes = .address
        return completer
    }()
func searchAddress(_ searchableText: String) {
        guard searchableText.isEmpty == false else { return }
        localSearchCompleter.queryFragment = searchableText
    }

Does someone have an Idea how can I solve this issue?)

I was trying using completer.region but it limited the results to this region only.

Upvotes: -1

Views: 349

Answers (1)

Ron Tabachnik
Ron Tabachnik

Reputation: 5

After spending a significant amount of time trying to resolve the issue, I was able to find a solution! Instead of trying to obtain the completer in English, I utilized some features provided by Apple.

First, I used the lookUpCurrentLocation function to retrieve the suggested location with the additional parameters of street, city, and country.

Inside this function, I utilized the getCoordinate function to retrieve the CLLocationCoordinate2D object and built a CLLocation based on the latitude and longitude.

To obtain the placemark in English, I then used CLGeocoder().reverseGeocodeLocation with the coordinates and preferredLocale parameter set to the desired language. This provided me with an accurate English (or any other language) placemark.

That's all! I hope this solution is helpful to others facing similar issues.

func lookUpCurrentLocation(street: String?,city: String,country: String,completionHandler: @escaping (CLPlacemark?,String?) -> Void ) {
    let geocoder = CLGeocoder()
    
    let lastLocation = getCoordinate(addressString: "\(street ?? ""), \(city), \(country)") { coordinates2D, error in
        if error != nil {
            completionHandler(nil,"\(error?.localizedDescription)")
        } else {
            let coordinate = CLLocation(latitude: coordinates2D.latitude, longitude: coordinates2D.longitude)
            geocoder.reverseGeocodeLocation(coordinate,preferredLocale: Locale(identifier: "en_US"),
                                            completionHandler: { (placemarks, error) in
                if error == nil {
                    let firstLocation = placemarks?[0]
                    print("Got address from String: \(firstLocation)")
                    completionHandler(firstLocation,nil)
                } else {
                    // An error occurred during geocoding.
                    completionHandler(nil,"\(error?.localizedDescription)")
                }
            })
        }
    }
}

Upvotes: -1

Related Questions