2

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"
}
antonio
  • 18,044
  • 4
  • 45
  • 61
Bagdat
  • 308
  • 4
  • 15
  • 1
    Querying https://maps.googleapis.com/maps/api/geocode/json?address=Imanova+19%2C+Astana using Chrome I receive `"lat" : 51.16052269999999` with `"location_type" : "APPROXIMATE"`. What is your `location_type`? – antonio Jul 11 '16 at 10:33
  • my location_type is ROOFTOP. I am also testing using Chrome. But why do we get different types? how to affect to this type? – Bagdat Jul 11 '16 at 11:43
  • According to https://developers.google.com/maps/documentation/geocoding/intro `ROOFTOP` "indicates that the returned result is a precise geocode for which we have location information accurate down to street address precision". I'm querying without API key – antonio Jul 11 '16 at 11:45
  • @antonio, but documentation says nothing about relation of API key and location_type – Bagdat Jul 11 '16 at 11:49
  • @antonio, can u plz show or send your json result where latitude is `51.16052269999999` – Bagdat Jul 11 '16 at 11:56
  • @antonio, very strange) when i follow your link, i.e. without API key, I am receiving the same result as before. Our results differs. Maybe it is locale affect. Many thanks, I will look further – Bagdat Jul 11 '16 at 12:20
  • Glad to hear that you finally succeeded! Could you post your solution as an answer and accept it so it can help others? – antonio Jul 20 '16 at 19:16
  • @antonio, i've solved problem by setting region. I've not properly searched in stackoverflow. [link](http://stackoverflow.com/questions/10302302/google-geocoding-api-giving-different-results-to-my-server-and-my-web-browser/10302440#10302440) [link](http://stackoverflow.com/questions/30748515/geocoding-a-specific-address-fails-from-php-works-from-browser-using-the-same-u) – Bagdat Jul 20 '16 at 19:17

1 Answers1

0

This answer https://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 know 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

Community
  • 1
  • 1
Bagdat
  • 308
  • 4
  • 15