2016-07-11 1 views
2

Ich bin mit diesem Java-libraraygoogle-maps-Dienste Java-Bibliothek Ergebnis unterscheidet sich von Browser-Aufruf Ergebnis

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

Und wenn ich den Code unter

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); 
} 

Ergebnis ausführen gibt

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

Aber wenn ich versuche, die gleiche Anfrage vom Browser aufzurufen, gibt es ein anderes Ergebnis.

enter image description here

Die Frage ist, warum so und wie sie zu beheben?

Bearbeiten aus den Kommentaren:

Ergebnis 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" 
} 
+1

Abfragen https://maps.googleapis.com/maps/api/geocode/json?address=Imanova+19%2C+Astana mit Chrome I erhalten '“ lat ": 51.16052269999999' mit' "location_type": "APPROXIMATE" '. Was ist dein 'location_type'? – antonio

+0

Mein Standorttyp ist ROOFTOP. Ich teste auch mit Chrome. Aber warum bekommen wir verschiedene Arten? Wie beeinflusst man diesen Typ? – Bagdat

+0

Gemäß https://developers.google.com/maps/documentation/geocoding/intro "ROOFTOP'" zeigt an, dass das zurückgegebene Ergebnis ein präziser Geocode ist, für den wir Ortsinformationen mit Genauigkeit bis zur Genauigkeit der Straßenadresse haben. " Ich frage ohne API-Schlüssel – antonio

Antwort

0

Diese Antwort https://stackoverflow.com/a/30749036/1862262 beschreibt vollständig die Lösung für mein Problem.

In gleicher Weise stellte ich Region

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

Und bekam das gleiche Ergebnis wie das Ergebnis im Browser.

Aber ich weiß nicht, welche Region es dauert, wenn es nicht festgelegt ist.

Hier ist die Dokumentation zu dieser Option und Problem https://developers.google.com/maps/documentation/geocoding/#RegionCodes