11

住所名があり、正確な緯度と経度を取得したいのですが。GeocoderのgetFromLocationName(address、maxresult)を使用してこれを取得できることはわかっています。

問題は、 https://maps.google.com/で得られる結果とは異なり、得られる結果は常にnullであるということです。これにより、ジオコーダーとは異なり、常にいくつかの結果を得ることができます。

また、別の方法を試しました:( "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"ここにリンクがあります!)ジオコーダーよりも良い結果が得られますが、多くの場合、エラーが返されます(java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)。それは退屈だ。

私の質問は、 Javaコード内からhttps://maps.google.com/で検索した場合と同じlatlong結果をどのように取得できるかということです。

追加: " http://maps.google.com/maps/api/geocode/json?address= " + address + "&sensor=false"の使用に関するAPIドキュメントはどこにありますか

4

6 に答える 6

20

アルバート、あなたの懸念はあなたのコードが機能していないことだと思います。以下のコードは私にとって非常にうまく機能します。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, puneSearchそれはより広範なリッチグーグルバックエンドを使用しているので、ウェブグーグルマップはあなたのためにそれを見つけます。自分のAPIから受信したGoogleAPIの唯一のリバースジオアドレス。インドの住所システムがいかに複雑であるかを考えると、私にはそれは合理的な作業方法のように思えます。2番目の交差点は1番目の交差点から何マイルも離れている可能性があります:)

于 2013-06-21T07:44:28.870 に答える
2

この機能を提供するWebサービスのリストは次のとおりです。

私のお気に入りの1つはこれです

打ってみませんか。

http://api.geonames.org/findNearbyPlaceName?lat=18.975&lng=72.825833&username=demo

これは次の出力を返します(必ず緯度と経度をURLに入れてください)

ここに画像の説明を入力してください

于 2012-06-26T08:00:53.170 に答える
2

これがお役に立てば幸いです。

public static GeoPoint getGeoPointFromAddress(String locationAddress) {
        GeoPoint locationPoint = null;
        String locationAddres = locationAddress.replaceAll(" ", "%20");
        String str = "http://maps.googleapis.com/maps/api/geocode/json?address="
                + locationAddres + "&sensor=true";

        String ss = readWebService(str);
        JSONObject json;
        try {

            String lat, lon;
            json = new JSONObject(ss);
            JSONObject geoMetryObject = new JSONObject();
            JSONObject locations = new JSONObject();
            JSONArray jarr = json.getJSONArray("results");
            int i;
            for (i = 0; i < jarr.length(); i++) {
                json = jarr.getJSONObject(i);
                geoMetryObject = json.getJSONObject("geometry");
                locations = geoMetryObject.getJSONObject("location");
                lat = locations.getString("lat");
                lon = locations.getString("lng");

                locationPoint = Utils.getGeoPoint(Double.parseDouble(lat),
                        Double.parseDouble(lon));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return locationPoint;
    }
于 2013-06-25T06:42:34.823 に答える
0

私は順番に使用します:
-Google Geocoding API v3(いくつかのケースでここで説明されている問題があります)
-Geocoder(いくつかのケースでここで説明されている問題があります)。

Google Geocoding APIから結果が得られない場合は、Geocoderを使用します。

于 2013-06-21T07:53:50.583 に答える
0

私が気付いた限りでは、GoogleマップはオートコンプリートにGoogle Places APIを使用しており、座標を取得できる場所の詳細を取得しています。

https://developers.google.com/places/documentation/ https://developers.google.com/places/training/

この方法では、期待どおりの結果が得られるはずです。

于 2013-06-26T09:52:56.733 に答える
-1
String locationAddres = anyLocationSpec.replaceAll(" ", "%20");
URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="+locationAddres+"&language=en&key="YourKey");
try(InputStream is = url.openStream(); JsonReader rdr = Json.createReader(is)) {
    JsonObject obj = rdr.readObject();
    JsonArray results = obj.getJsonArray("results");
    JsonObject geoMetryObject, locations;

    for (JsonObject result : results.getValuesAs(JsonObject.class)) {
        geoMetryObject=result.getJsonObject("geometry");
        locations=geoMetryObject.getJsonObject("location");
        log.info("Using Gerocode call - lat : lng value for "+ anyLocationSpec +" is - "+locations.get("lat")+" : "+locations.get("lng"));
        latLng = locations.get("lat").toString() + "," +locations.get("lng").toString();
    }
}
于 2018-10-23T15:31:57.953 に答える