• ホーム
  • HS品目表
  • 日本地図
  • 世界地図
  • タイムライン
  • カレンダー
  • お問い合わせ
  • 運営者情報

HTS macam-macam

  • ホーム
  • HS品目表
  • 日本地図
  • 世界地図
  • タイムライン
  • カレンダー
  • お問い合わせ
  • 運営者情報

GoogleMap API ジオコーダーを Java で使う

Javascript からの Geocorder の利用が、1回で連続して変換できるのが 10件 に制限されているので、大量データの変換に不向きなので、調べて実装しました。



package mapaddress.dbupdate;

import java.util.*;
import java.io.*;

import com.google.maps.GeoApiContext;
import com.google.maps.*;
import com.google.maps.model.GeocodingResult;
import com.google.maps.model.LatLng;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import org.apache.ibatis.session.SqlSession;

//import com.google.maps.GeoApiContextBuilder;
/**
 * GoogleMap API ジオコーダー用クラス
 */
public class Geocorder {

  /**
   * プロキシ設定
   */
  private final String PROXY_HOST = "?????????";
  private final int PROXY_PORT = 0;

  private final String API_KEY = "???????????????????????????????";

  private GeoApiContext context;

  private String addr;
  private double lat;
  private double lon;
  private String zip = "";

  /**
   * デフォルトコンストラクタ
   */
  public Geocorder() {
    context = new GeoApiContext.Builder()
            .apiKey(API_KEY)
            .build();
  }

  /**
   * APIキー指定コンストラクタ
   */
  public Geocorder(String apikey) {
    context = new GeoApiContext.Builder()
            .apiKey(apikey)
            .build();
  }

  /**
   * プロキシ用コンストラクタ
   *
   * @param isUseProxy
   */
  public Geocorder(boolean isUseProxy) {

    if (isUseProxy) {
      SocketAddress addr = new InetSocketAddress(this.PROXY_HOST, this.PROXY_PORT);
      Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
      context = new GeoApiContext.Builder()
              .apiKey(API_KEY)
              .proxy(proxy)
              .build();
    }
    else {
      context = new GeoApiContext.Builder()
              .apiKey("????????????????????????????????????????")
              .build();
    }
  }

  /**
   * @return the addr
   */
  public String getAddr() {
    return addr;
  }

  /**
   * @param addr the addr to set
   */
  public void setAddr(String addr) {
    this.addr = addr;
  }

  /**
   * @return the lat
   */
  public double getLat() {
    return lat;
  }

  /**
   * @param lat the lat to set
   */
  public void setLat(double lat) {
    this.lat = lat;
  }

  /**
   * @return the lon
   */
  public double getLon() {
    return lon;
  }

  /**
   * @param lon the lon to set
   */
  public void setLon(double lon) {
    this.lon = lon;
  }

  /**
   * @return the zip
   */
  public String getZip() {
    return zip;
  }

  /**
   * @param zip the zip to set
   */
  public void setZip(String zip) {
    this.zip = zip;
  }

  /**
   * 正ジオコーダーの実行
   */
  public void execGeoCorder() {

    try {
      GeocodingResult results[] = this.getResults(this.addr);
      //LatLng latLng = results[0].geometry.location; // とりあえず一番上のデータを使う

      if (results != null && results.length > 0) {
        //if (results.length > 0) {
        LatLng latLng = results[0].geometry.location; // とりあえず一番上のデータを使う
        System.out.println("緯度 : " + latLng.lat);
        System.out.println("経度 : " + latLng.lng);
        int len = results[0].addressComponents.length;
        String zip = results[0].addressComponents[len - 1].longName;
        System.out.println("ZIP : " + zip);

        this.lat = latLng.lat;
        this.lon = latLng.lng;
        if (zip != null && !zip.equals("")) {
          this.zip = zip;
        }

      }

    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }

  /**
   * 正ジオコーダー応答取得
   *
   * @param address
   * @return
   * @throws InterruptedException
   * @throws IOException
   */
  public GeocodingResult[] getResults(String address) throws InterruptedException, IOException {

    GeocodingApiRequest req = GeocodingApi.newRequest(context)
            .address(address)
            // .components(ComponentFilter.country("JP"))
            .language("ja");

    try {
      GeocodingResult[] results = req.await();
      if (results == null || results.length == 0) {
        // ZERO_RESULTSはresults.length==0の空配列がsuccessful扱いで返ってくる
        System.out.println("zero results.");
      }
      //results[0].geometry;

      return results;

    }
    catch (Exception e) {
      System.out.println("error.");
      System.out.println(e);
      return null;
    }
  }

  /**
   * 逆ジオコーダーの実行
   */
  public void execRVGeoCorder(String lat_lon) {
    
      try {
      GeocodingResult results[] = this.getRVResults(lat_lon);
      //LatLng latLng = results[0].geometry.location; // とりあえず一番上のデータを使う

      if (results != null && results.length > 0) {
        
        int len = results[0].addressComponents.length;
        String zip = results[0].addressComponents[len - 1].longName;
        System.out.println("ZIP : " + zip);
        this.addr = results[0].formattedAddress.replaceAll("日本、", "");
        System.out.println("ADDR : " + this.addr);
        if (zip != null && !zip.equals("")) {
          this.zip = zip;
        }

      }

    }
    catch (Exception e) {
      e.printStackTrace();
    }


  }

  public GeocodingResult[] getRVResults(String lat_lon) throws InterruptedException, IOException {
    
    

    GeocodingApiRequest req = GeocodingApi.newRequest(context)
            .address(lat_lon)
            // .components(ComponentFilter.country("JP"))
            .language("ja");

    try {
      GeocodingResult[] results = req.await();
      if (results == null || results.length == 0) {
        // ZERO_RESULTSはresults.length==0の空配列がsuccessful扱いで返ってくる
        System.out.println("zero results.");
      }
      //results[0].geometry;

      return results;

    }
    catch (Exception e) {
      System.out.println("error.");
      System.out.println(e);
      return null;
    }
  }

  public static void main(String[] args) {
    
    Geocorder G = new Geocorder(false);  
    G.execRVGeoCorder("31.62457 131.8521498");
    System.out.println(G.addr);
    


  }

}

ここで教えていただきました : http://developers.goalist.co.jp/entry/2017/10/16/150000

逆ジオコーダーも必要あるので、試したところ、Javascript の住所指定と同じく、緯度 + ” ” + 経度 を住所として渡せば出来ました。

共有:

  • クリックして Twitter で共有 (新しいウィンドウで開きます)
  • Facebook で共有するにはクリックしてください (新しいウィンドウで開きます)

関連

2019-04-26 API Geocorder googleMap Java
コメントはまだありません

船スケジュール 地図パート 配備録

MySQL LOAD DATA LOCAL INFILE で特定列を指定してインポートする

コメントを残す コメントをキャンセル

コメントを投稿するにはログインしてください。

カテゴリー

  • API (16)
    • FORSQUARE (1)
    • Google Vision (1)
    • googleMap (4)
      • Geocorder (1)
    • Holidays (1)
    • IP GeoLocation (1)
    • Leaflet (2)
    • OpenCage Geocorder (1)
    • RESAS (内閣府 地方創生推進室) (2)
    • teraren.com (1)
    • Yahoo!スタティックマップAPI (1)
  • Database (36)
    • Firebird (4)
    • MySQL (32)
    • Oracle (2)
    • PostgreSQL (2)
    • SQLAnyWhere (1)
    • sqlite (3)
  • Info Site (3)
    • Geomedian (1)
    • 地図蔵 (1)
    • 開店閉店.com (1)
  • Java (43)
    • groovy (2)
    • gson (9)
    • jps jstat (1)
    • JSoup (2)
    • JSP (4)
    • MyBatis (8)
    • Reflection (1)
    • Selenium (2)
    • Servlet (3)
    • Swing (1)
    • Tomcat (7)
  • JavaScript (63)
    • Class (2)
    • dropzone (1)
    • iziToast (1)
    • jqGrid (26)
    • jQuery (7)
    • jQuery-ui (17)
      • AutoComplete (1)
      • conTextMenu (1)
      • Dialog (3)
      • LightBox (2)
      • Lity (1)
      • multipleSelect (5)
      • Slick (1)
    • JSON (1)
    • Numeral (2)
    • Tippy (2)
    • toastr (1)
  • Linux (16)
    • Apache (5)
    • bash (1)
    • CentOS (3)
    • curl (1)
    • Fedora (1)
    • inotify (1)
    • jq (1)
  • PHP (16)
    • GD (11)
    • PDO (2)
  • Public Data (1)
    • 郵便番号 (1)
  • Tools (16)
    • favicon抽出 (1)
    • google-images-download (1)
    • Heidi SQL (1)
    • HTML (1)
    • HttpFox (1)
    • Javascript Obfuscator (1)
    • JSON Pretty Liner (1)
    • SSL Check (1)
    • Unix Timestamp (1)
    • whois geoIP (1)
    • XShell (1)
    • ZBarImg (1)
    • フリーアイコン (1)
    • 画像 (2)
  • Trouble (9)
  • Windows (2)
    • PowerShell (1)
    • コマンドプロンプト (1)
  • WordPress (1)
  • アプリケーション (4)
  • バドミントン (3)
  • リリースニュース (7)
  • 偉人名言集 (42)
    • Steeve Jobs (1)
    • アインシュタイン (1)
    • 三島由紀夫 (2)
    • 井上ひさし (1)
    • 井深大 (1)
    • 夏目漱石 (1)
    • 大隈重信 (4)
    • 小松左京 (1)
    • 山本五十六 (1)
    • 手塚治虫 (1)
    • 星新一 (1)
    • 本田宗一郎 (3)
    • 杉原千畝 (1)
    • 松本清張 (1)
    • 樋口一葉 (1)
    • 武者小路実篤 (1)
    • 湯川秀樹 (1)
    • 石ノ森 章太郎 (1)
    • 福沢諭吉 (14)
    • 赤塚不二夫 (2)
  • 情報システム (3)
  • 書評 (1)
  • 未分類 (1)
  • 海上輸送 (3)
  • 軍事研究 (2)
    • 宇宙 (2)
  • 連絡 (3)

アーカイブ

  • 2020年5月
  • 2020年3月
  • 2020年2月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月

最近の投稿

  • date.nager.at (祝日)
  • Javascript ES2015 で連想配列を検索
  • jQuery Chosen でセレクトボックス
  • 輸出入品目表
  • 日本地図

翻訳

Proudly powered by WordPress | テーマ: Neblue by NEThemes.