google-maps-services-java icon indicating copy to clipboard operation
google-maps-services-java copied to clipboard

How to format and encode address when using GeocodingApi ?

Open YLombardi opened this issue 3 years ago • 2 comments

I'm working on a Spring application that uses google-maps-service 2.0.0. I try to use the GeocodingApi but I don't understand how to format the address. Do you know if there is a documentation that explains how to use this ?

In the official Google documentation, there is exemples for the javascript api, like this : https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

And the Java documentation redirect to this repository, where I found this exemple :

GeocodingResult[] results =  GeocodingApi.geocode(context,
    "1600 Amphitheatre Parkway Mountain View, CA 94043").await();

Here is some exemple I tried, I'm exepecting the api to return something like "Chambéry, France" or "Savoie, France" :

// Full address without encoding
GeocodingApi.geocode(context, "FR 73000 Chambéry").region("FR").await()
// result[0].addressComponents => 73000 Chambéry, France => OK
// Full address with spaces encoding
GeocodingApi.geocode(context, "FR+73000+Chambéry").region("FR").await()
// result[0].addressComponents => 73000 Chambéry, France => OK
// Full address with full utf8 encoding
GeocodingApi.geocode(context, "FR+73000+Chamb%C3%A9ry").region("FR").await()
// result[0].addressComponents => France => KO, I shouldn't encode special chars in city names.

Here it seems that the encoding of spaces isn't necessary and special chars should not be encoded.

// Zip code and country code
GeocodingApi.geocode(context, "FR 73").region("FR").await()
// result[0] => null 
// Country code and zip code
GeocodingApi.geocode(context, "73 FR").region("FR").await()
// result[0] => null 
// Zip code and country code with spaces encoding
GeocodingApi.geocode(context, "FR+73").region("FR").await()
// result[0].addressComponents => Savoie, France => OK
// Country code and zip code with spaces encoding
GeocodingApi.geocode(context, "73+FR").region("FR").await()
// result[0].addressComponents => 6673+FR La Celle-sous-Gouzon, France => Wrong result

Here it seems that encoding the spaces is necessary, and I need to put the Country code before the zip code.

Can I assume that it is always the good pratice to do this ? (encoding spaces and always have the country code first)

YLombardi avatar Apr 27 '22 15:04 YLombardi

@YLombardi Thank you for opening this issue. 🙏 Please check out these other resources that might be applicable:

This is an automated message, feel free to ignore.

jpoehnelt avatar Apr 27 '22 15:04 jpoehnelt

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!

stale[bot] avatar Sep 21 '22 04:09 stale[bot]

Indeed, you should encode the address. The region parameter is optional. See related documentation and URL encoding tips.

wangela avatar Jan 30 '23 22:01 wangela