2016-05-04 8 views
2

Ich versuche, die Land- und Stadtnamen von Breitengrad und Longitude mit Google Geocoding API zu bekommen. Diese Bibliothek https://github.com/googlemaps/google-maps-services-java als JAVA-Implementierung für die API.JAVA google geocoding API get city

Dies ist die aktuelle Art, wie ich es mache:

GeoApiContext context = new GeoApiContext().setApiKey("AI... my key"); 
GeocodingResult[] results = GeocodingApi.newRequest(context) 
     .latlng(new LatLng(40.714224, -73.961452)).language("en").resultType(AddressType.COUNTRY, AddressType.ADMINISTRATIVE_AREA_LEVEL_1).await(); 

logger.info("Results lengh: "+ results.length); 

for(int i =0; i< results[0].addressComponents.length; i++) { 
    logger.info("Address components "+i+": "+results[0].addressComponents[i].shortName); 
} 

Das Problem ist: Es ist ein 5-Stufen-AddressType.ADMINISTRATIVE_AREA_LEVEL_1 und Stadtname ist auf verschiedenen Ebenen ist abhängig von bestimmten Ort/Land. Die Frage ist also - wie kann ich genau den Stadtnamen aus den Ergebnissen extrahieren? oder wie muss ich eine Anfrage richtig formulieren?

P.S. Es ist keine mobile App.

+0

'administrative_area_level_2' Stadt sein sollte –

+0

, wie ich vor erwähnt, für die verschiedenen Länder/Standorte Stadt bis auf gezeigt abweichend "administrative_area_level_2" (1-5). – user1935987

+0

bieten Beispiele, geben Sie mir die vollständige URL außer API-Schlüssel, ich habe meine eigenen API-Schlüssel, ich werde auch treffen. :) –

Antwort

2

Verwenden AddressComponentType.LOCALITYcity name von GeocodingResult

bekommen ich es auf diese Weise tun:

private PlaceName parseResult(GeocodingResult r) { 

    PlaceName placeName = new PlaceName(); // simple POJO 

    for (AddressComponent ac : r.addressComponents) { 
     for (AddressComponentType acType : ac.types) { 

      if (acType == AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_1) { 

       placeName.setStateName(ac.longName); 

      } else if (acType == AddressComponentType.LOCALITY) { 

       placeName.setCityName(ac.longName); 

      } else if (acType == AddressComponentType.COUNTRY) { 

       placeName.setCountry(ac.longName); 
      } 
     } 

     if(/* your condition */){ // got required data 
      break; 
     } 
    } 

    return placeName; 
} 
+0

danke ich denke, das ist gut genug für mich. – user1935987