Albert, credo che la vostra preoccupazione è che il codice non funziona. Ecco, il codice qui sotto funziona davvero bene per me. Penso che manchi URIUtil.encodeQuery
per convertire la stringa in un URI.
Sto usando la libreria gson, scaricalo e aggiungilo al tuo percorso.
Per ottenere la classe per l'analisi gson, è necessario passare a jsonschema2pojo. Basta sparare http://maps.googleapis.com/maps/api/geocode/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=true sul tuo browser, ottenere i risultati e incollarlo su questo sito. Genererà il tuo pojo per te. Potrebbe essere necessario aggiungere anche un annotation.jar.
Fidati di me, è facile lavorare. Non essere frustrato ancora.
try {
URL url = new URL(
"http://maps.googleapis.com/maps/api/geocode/json?address="
+ URIUtil.encodeQuery("Sayaji Hotel, Near balewadi stadium, pune") + "&sensor=true");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output = "", full = "";
while ((output = br.readLine()) != null) {
System.out.println(output);
full += output;
}
PincodeVerify gson = new Gson().fromJson(full, PincodeVerify.class);
response = new IsPincodeSupportedResponse(new PincodeVerifyConcrete(
gson.getResults().get(0).getFormatted_address(),
gson.getResults().get(0).getGeometry().getLocation().getLat(),
gson.getResults().get(0).getGeometry().getLocation().getLng())) ;
try {
String address = response.getAddress();
Double latitude = response.getLatitude(), longitude = response.getLongitude();
if (address == null || address.length() <= 0) {
log.error("Address is null");
}
} catch (NullPointerException e) {
log.error("Address, latitude on longitude is null");
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Le opere Geocode http, ho appena sparato esso, risultati qui sotto
{
"results" : [
{
"address_components" : [
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Maharashtra",
"short_name" : "MH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Pune, Maharashtra, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.63469650,
"lng" : 73.98948670
},
"southwest" : {
"lat" : 18.41367390,
"lng" : 73.73989109999999
}
},
"location" : {
"lat" : 18.52043030,
"lng" : 73.85674370
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.63469650,
"lng" : 73.98948670
},
"southwest" : {
"lat" : 18.41367390,
"lng" : 73.73989109999999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Modifica
On 'risposte non contengono abbastanza detail`
Da tutte le ricerche ti aspetti che la mappa di Google abbia un riferimento a ogni combinazione di località, area, città.Ma resta il fatto che la mappa di google contiene geo e reverse-geo nel proprio contesto. Non puoi aspettarti che abbia una combinazione come Sayaji Hotel, Near balewadi stadium, pune
. Web google maps lo localizzerà per te poiché utilizza un backend ricco di google Search
ricco. L'unico indirizzo geografico inverso di google api è stato ricevuto dalle proprie API. Per me sembra un modo ragionevole per lavorare, considerando quanto sia complesso il nostro sistema di indirizzi indiano, 2 ° incrocio può essere miglia lontano da 1st Cross road :)
GeoCoder non è una scienza perfetta. Ricordatelo. Ad esempio volevo "Wadgaon sheri" e mi mostra la strada principale di Kharadi vicino al parco EON IT. Quindi ho aggiunto "l'ufficio postale" all'indirizzo. E invece di "wadgaon sheri" ho usato il codice PIN. Per farla breve, non aspettarti che Geocodeing faccia magia per te. È probabile che tutte le API che utilizzi finiranno per ottenere gli stessi risultati, dal momento che anche underneat useranno il geocoding. – Siddharth