LoginSignup
0
0

More than 3 years have passed since last update.

GoogleMapsAPIのJavaSDKを使って、逆GeoCodingの結果を日本語で取得する。

Posted at

はじめに

GoogleMapsAPIを使って、緯度経度から住所を取得する処理を書きました。
GoogleからJavaのSDKが提供されています。
日本語で住所を返す設定のメモ書きです。

JavaのSDK

  • Java Client for Google Maps Services

ソースコード

    public String reverceGeocoding(double lat, double lon) {
        LatLng latlng = new LatLng(lat, lon);

        GeoApiContext context = new GeoApiContext.Builder().apiKey(apikey).build();
        GeocodingApiRequest request = GeocodingApi.reverseGeocode(context, latlng).language("ja");
        GeocodingResult[] results;
        String address = null;
        try {
            results = request.await();
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            address = gson.toJson(results[0].formattedAddress);
        } catch (ApiException | InterruptedException | IOException e) {
            e.printStackTrace();
        } finally {
            context.shutdown();
        }
        return address;
    }

おわりに

作ってしまえばなんてことないのですが、languageの指定方法は調査しました。覚え書きとして残しておきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0