アルバート、あなたの懸念はあなたのコードが機能していないことだと思います。以下のコードは私にとって非常にうまく機能します。URIUtil.encodeQuery
文字列をURIに変換するのに欠けていると思います。
私はgsonライブラリを使用しています。ダウンロードして、パスに追加してください。
gson解析用のクラスを取得するには、jsonschema2pojoに移動する必要があります。ブラウザでhttp://maps.googleapis.com/maps/api/geocode/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=trueを起動し、結果を取得してこのサイトに貼り付けるだけです。それはあなたのためにあなたのpojoを生成します。また、annotation.jarを追加する必要がある場合があります。
私を信じてください、仕事を始めるのは簡単です。まだイライラしないでください。
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();
}
Geocode httpは機能します、私はちょうどそれを解雇しました、以下の結果
{
"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"
}
編集
'回答に十分な詳細が含まれていません`
あなたが期待しているすべての研究から、グーグルマップは地域、地域、都市のすべての組み合わせへの参照を持っています。しかし、グーグルマップには独自のコンテキストでgeoとreverse-geoが含まれているという事実は変わりません。のような組み合わせは期待できませんSayaji Hotel, Near balewadi stadium, pune
。Search
それはより広範なリッチグーグルバックエンドを使用しているので、ウェブグーグルマップはあなたのためにそれを見つけます。自分のAPIから受信したGoogleAPIの唯一のリバースジオアドレス。インドの住所システムがいかに複雑であるかを考えると、私にはそれは合理的な作業方法のように思えます。2番目の交差点は1番目の交差点から何マイルも離れている可能性があります:)