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


in 1 of apps need add ability find city name. using clgeocoder achieve , want have behaviour identical ios weather app.

below code have:

clgeocoder().geocodeaddressstring(searchbar.text!, completionhandler:{ (placemarks, error) -> void in     guard let nonnilmarks = placemarks else {return}     placemark in nonnilmarks {         print("locality: \(placemark.locality)")         print("name: \(placemark.name)")         print("country: \(placemark.country)")         print("formatted address: \(placemark.addressdictionary)")     } }) 

it works in cases. however, noticed cases when fails. below output examples:

searching 'milan' - works

locality: optional("milan") name: optional("milan") country: optional("italy") formatted address: optional([subadministrativearea: milan, state: lombardy, countrycode: it, country: italy, name: milan, formattedaddresslines: (     milan,     italy ), city: milan]) 

this correct output

searching 'italy, tx' - works

there town in texas called italy. if type 'italy, tx' output is:

locality: optional("italy") name: optional("italy") country: optional("united states") formatted address: optional([subadministrativearea: ellis, state: tx, countrycode: us, country: united states, name: italy, formattedaddresslines: (     "italy, tx",     "united states" ), city: italy]) 

searching 'italy' - fails

when type 'italy' place mark country:

locality: nil name: optional("italy") country: optional("italy") formatted address: optional([countrycode: it, name: italy, formattedaddresslines: (     italy ), country: italy]) 

searching 'singapore, singapore' - works

this similar case 'italy, tx':

locality: optional("singapore") name: optional("singapore") country: optional("singapore") formatted address: optional([city: singapore, countrycode: sg, name: singapore, state: singapore, formattedaddresslines: (     singapore,     singapore ), country: singapore]) 

searching 'singapore' - fails

again, seems similar case 'italy'. finds country:

locality: nil name: optional("singapore") country: optional("singapore") formatted address: optional([countrycode: sg, name: singapore, formattedaddresslines: (     singapore ), country: singapore]) 

preliminary guess

at stage though maybe geocodeaddressstring: stops searching if finds country name equal search parameter, tried changing code :

let addrdict = [kabpersonaddresscitykey nsstring: searchbar.text!] clgeocoder().geocodeaddressdictionary(addrdict, completionhandler:{ (placemarks, error) -> void in     print(placemarks) }) 

i thought restricting search term city name correct results. unfortunately, exact same behaviour!

and then realised have problems not cases when city name equal country name.

searching 'tokyo' - epic fail

locality: nil name: optional("tokyo") country: optional("japan") formatted address: optional([countrycode: jp, name: tokyo, state: tokyo, formattedaddresslines: (     tokyo,     japan ), country: japan]) 

conclusion

not can clgeocoder omit results (like in case of 'italy') can tell me place not have locality when, in fact, (i believe last time checked map tokyo still city!).

does know how can resolve of these issues?

weather app somehow - searching 'singapore' or 'italy' there gives correct results. grateful help!

p.s. although using swift adding objective-c tag think question not swift specific.

usage showed right , don't think there else can improve results. still complicated algorithm tries sort results based on set of rules , assumption.

that being sad, understand frustration because i've been there , done that. problem geocoding database not complete , apple not company leads field - google is. here can see technical note when geocoder introduced saying there still countries not supported, or partially supported.

so suggestion if want best results (though still not fail-proof), switch google geolocation. quite great amount of requests free , after costs minor amounts. success rate around 99% basic searches experienced , gets better if provide more information. api options quite extensive.

here links developer documentation both geocoding , reverse geocoding: https://developers.google.com/maps/documentation/geocoding/intro https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

hope helped @ least little.

edit: after wrote did little research , seems not person make claim (also including same unsupported link).


Comments