CLGeocoder returns wrong results when city name is equal to some country's name (and not only)

Usage what you showed is completely right and I don't think there is anything else you can do to improve results. It is still just some complicated algorithm that tries to sort results based on set of rules and some assumption.

That being sad, I understand your frustration completely because I've been there and done that. The problem is that Geocoding database is not obviously complete and Apple is not the company who leads this field - Google is. Here you can see technical note from when the Geocoder was introduced saying that there are still countries that are not supported, or just partially supported.

So my suggestion to you is that if you want to get best results (though still not fail-proof), switch to Google Geolocation. For quite great amount of requests it is free and after that it costs some very minor amounts. The success rate is around 99% for all the basic searches from what I experienced and it only gets better if you provide more information. The API options are also quite extensive.

Here are links for developer documentation for both Geocoding and Reverse Geocoding: https://developers.google.com/maps/documentation/geocoding/intro https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

Hope it helped at least a little.

EDIT: After I wrote this I did a little research and it seems that I am not the only person to make this claim (also including the same unsupported link).


CLGeocoder requests are rate limited and if the app goes over the threshold, further requests may not be fulfilled. CLGeocoder requires network connection which may be limiting under some circumstances and the response from the server is not always instant. If you do not insist on CLGeocoder, local database might give you more flexibility and possibly better user experience.

Take a look at the cities???.zip on GeoNames, for an alternative solution where you would import the data to a local database.


You can also do this by google API like this

//chakshu

       var urlString = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=\(searchString)&sensor=true&key=\(Google_Browser_Key)"

                var linkUrl:NSURL = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)!

                if(countElements(urlString)>0 && !urlString.isEmpty)
                {
                   // if let fileData = String(contentsOfURL: NSURL(string: urlString)!, encoding: NSUTF8StringEncoding, error: nil)
                    if let fileData = String(contentsOfURL: linkUrl, encoding: NSUTF8StringEncoding, error: nil)
                    {
                        println(fileData)

                        var data = fileData.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: false)
                        var localError: NSError?
                        if(data != nil)
                        {
                            var json: AnyObject! = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &localError)

}
}

}