Bagdat Bagdat - 4 months ago 31
Java Question

google-maps-services java library result differs from browser call result

I am using this java libraray

<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.15</version>
</dependency>


And when I execute the code below

public static GeocodingResult getGeocode(String address) {
GeocodingResult[] results;
try {
GeoApiContext context = new GeoApiContext().setApiKey(GOOGLE_KEY);
results = GeocodingApi.geocode(context, address).await();
if (results.length > 0) {
return results[0];
} else {
return null;
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

public static void main(String[] args) {
GeocodingResult geocode = getGeocode("Imanova 19, Astana");

System.out.println(geocode.geometry.location.lat);
}


Result gives

INFO: Request: https://maps.googleapis.com/maps/api/geocode/json?key=MY_GOOGLE_KEY&address=Imanova+19%2C+Astana
51.16052269999999


But, when I try to call the same given request from browser, it gives another result.

enter image description here

The question is, why so and how to fix?

Edit from the comments:

Result of https://maps.googleapis.com/maps/api/geocode/json?address=Imanova+19%2C+Astana

{
"results" : [
{
"address_components" : [
{
"long_name" : "Astaná",
"short_name" : "Astaná",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Astana",
"short_name" : "Astana",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Kazajistán",
"short_name" : "KZ",
"types" : [ "country", "political" ]
},
{
"long_name" : "020000",
"short_name" : "020000",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Astaná 020000, Kazajistán",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.2903453,
"lng" : 71.7427397
},
"southwest" : {
"lat" : 51.0055461,
"lng" : 70.9179879
}
},
"location" : {
"lat" : 51.16052269999999,
"lng" : 71.47035579999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.2903453,
"lng" : 71.7427397
},
"southwest" : {
"lat" : 51.0055461,
"lng" : 70.9179879
}
}
},
"partial_match" : true,
"place_id" : "ChIJCUa1fcSARUIRKJKx3Y0U-Zc",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}

Answer

This answer http://stackoverflow.com/a/30749036/1862262 fully describes the solution for my problem.

In the same way I set region

GeocodingResult[] results = GeocodingApi.geocode(context, address).region("KZ").await();

And got the same result as the result in browser.

But I don't what region it takes if it's not set.

Here is documentation about this option and problem https://developers.google.com/maps/documentation/geocoding/#RegionCodes

Comments