C++ で住所などから緯度・経度を求める

(1/1)
>C++で住所などから緯度・経度を求める
インターネット上のクラウドサービスを利用し、住所やランドマークなどから緯度・経度を検索するアプリケーションである。検索結果をクリプボードにコピーしたり、テキストファイルに保存することができる。また、ユーザーがAPIキーを取得することで、GoogleやYahoo!JAPANのサービスを利用することができる。
PHPで住所・ランドマークから緯度・経度を求める」で作ったPHPプログラムをC++に移植したものである。

(2025年3月16日)ネット接続チェック強化,使用ライブラリ更新
(2024年11月30日)使用ライブラリ更新
(2024年8月17日)使用ライブラリ更新
(2024年3月23日)使用ライブラリ更新

目次

サンプル・プログラム

圧縮ファイルの内容
address2geowin.msiインストーラ
bin/address2geowin.exe実行プログラム本体
bin/libcurl-x64.dll実行時に必要になるDLL
bin/etc/help.chmヘルプ・ファイル
sour/address2geowin.cppソース・プログラム
sour/resource.hリソース・ヘッダ
sour/resource.rcリソース・ファイル
sour/application.icoアプリケーション・アイコン
sour/mystrings.cpp汎用文字列処理関数など(ソース)
sour/mystrings.h汎用文字列処理関数など(ヘッダ)
sour/pahooGeocode.cpp住所・緯度・経度に関わるクラス(ソース)
sour/pahooGeocode.cpp住所・緯度・経度に関わるクラス(ヘッダ)
sour/apikey.cppAPIキーの管理(ソース)
sour/apikey.hppAPIキーの管理(ヘッダ)
sour/makefileビルド
address2geowin.cpp 更新履歴
バージョン 更新日 内容
1.3.0 2025/03/15 Leafletアクセス可否チェック追加,使用ライブラリ更新
1.2.4 2024/11/30 使用ライブラリ更新
1.2.3 2024/08/17 使用ライブラリ更新
1.2.2 2024/03/23 使用ライブラリ更新
1.2.1 2023/11/11 使用ライブラリ更新
pahooGeocode.cpp 更新履歴
バージョン 更新日 内容
1.9.0 2025/03/16 HTTPステータス・エラーをキャッチアップ
1.8.1 2025/02/23 setError() -- bug-fix
1.8.0 2024/05/03 getMyPath()をapikey.appのgetMyPath()関数に変更
1.7.0 2024/04/27 Edge対応に伴いデバッグ用コードを廃棄
1.6.0 2023/07/02 getPointsGSI()メソッド追加
mystrings.cpp 更新履歴
バージョン 更新日 内容
1.3.1 2025/03/16 readWebContents() リダイレクト有効に
1.3.0 2025/03/16 readWebContents() 引数httpStatus追加
1.2.0 2024/05/06 getModulePath() 追加
1.12 2021/01/31 readWebContents() 引数post追加
1.11 2020/10/17 htmlspecialchars() 追加

使用ライブラリ

クラウドサービスにアクセスするために、オープンソースのライブラリ Boost C++ライブラリcURL (カール)  および OpenSSL が必要になる。導入方法等については、「C++ 開発環境の準備」をご覧いただきたい。

リソースの準備

今回は32ビット版の開発環境を用いる。
Eclipse を起動し、新規プロジェクト address2geowin を用意する。
ResEdit を起動し、resource.rc を用意する。
Eclipse に戻り、ソース・プログラム "address2geowin.cpp" を追加する。
リンカー・フラグを -mwindows -static -lstdc++ -lgcc -lwinpthread -lcurl -lssl -lole32 "C:\pleiades\eclipse\mingw\mysys2\mingw32\bin\libcurl.dll" に設定する。

MSYS2 コマンドラインからビルドするのであれば、"makefile" を利用してほしい。

解説:定数など

address2geowin.cpp

  36: // 定数など ==================================================================
  37: #define MAKER       "pahoo.org"             // 作成者
  38: #define APPNAME     "address2geowin"        // アプリケーション名
  39: #define APPNAMEJP   "住所などから緯度・経度を求める"
  40:                                             // アプリケーション名(日本語)
  41: #define APPVERSION  "1.3.0"                 // バージョン
  42: #define APPYEAR     "2020-25"               // 作成年
  43: #define REFERENCE   "https://www.pahoo.org/e-soul/webtech/cpp01/cpp01-14-01.shtm"   // 参考サイト
  44: 
  45: // ヘルプ・ファイル
  46: #define HELPFILE    ".\\etc\\help.chm"
  47: 
  48: // デフォルト保存ファイル名
  49: #define SAVEFILE    "address2geowin.txt"
  50: 
  51: // 現在のインターフェイス
  52: HINSTANCE hInst;
  53: 
  54: // アプリケーション・ウィンドウ
  55: HWND hParent;
  56: 
  57: // アプリケーション・ウィンドウ位置
  58: unsigned hParent_X, hParent_Y;
  59: 
  60: // 検索キー格納用
  61: string Query;
  62: 
  63: // エラー・メッセージ格納用【変更不可】
  64: string ErrorMessage;
  65: 
  66: // UserAgent
  67: string UserAgent;
  68: 
  69: // pahooGeocodeオブジェクト
  70: pahooGeocode *pGC;

とくに注意記載が無い限り、定数は自由に変更できる。

解説:クラス

C++には、オブジェクト指向プログラムミングを行うための仕組みとしてクラスが用意されている。ある程度まとまった処理を別プログラムで再利用したり、複雑なデータ構造を扱う際、クラスを使うと効果がある。
今回は、クラウドサービスにアクセする処理を pahooGeocode クラス としてコーディングした。ソースは "pahooGeocode.cpp" ヘッダは "pahooGeocode.hpp" である。

pahooGeoCode.cpp

  31: using namespace std;
  32: using namespace boost;
  33: using namespace boost::property_tree;
  34: 
  35: /**
  36:  * コンストラクタ
  37:  * @param   string appname アプリケーション名
  38:  */
  39: pahooGeocode::pahooGeocode(std::string appname) {
  40:     this->appname = appname;
  41:     this->readGoogleApiKey();
  42:     // ホットペッパーグルメWebサービス APIキー読み込み
  43:     readApiKey(FNAME_YAHOO_API, &this->YahooAPIkey);
  44: }
  45: 
  46: // デストラクタ
  47: pahooGeocode::~pahooGeocode() {
  48: }
  49: 
  50: // 各種処理 ==================================================================

クラス名と同じ名前を持つメソッドはコンストラクタと呼び、クラスからオブジェクトを生成するときに最初に呼ばれる。
ここでは、GoogleAPIキーやYahoo!アプリケーションIDの読み込みを行う。
一方、クラス名に "~" を付けたメソッドはデストラクタと呼び、オブジェクトを解放するときに呼ばれる。
このデストラクタは何もしない。

解説:住所検索サービスAPI

pahooGeocode クラスは、下表の住所検索サービスAPIを選択して呼び出すことが可能である。各々のAPIの仕様については、「PHPで住所・ランドマークから緯度・経度を求める」をご覧いただきたい。
住所検索サービス
サービス名 メソッド 制 約
1 Google getPointsGoogle 有料(決められた無料枠あり)。全世界の住所、ランドマークの検索可能。
2 Yahoo!JAPAN getPointsYOLP 無料(?)。住所、ランドマーク、海外のどれを検索するか指定。ランドマークや海外地名検索はGoogleに劣る。
11 HeartRails Geo API getPointsHRG 無料。住所、または住所の一部のみ検索可能。
12 OSM Nominatim Search API getPointsNominatim 無料。全世界の住所、ランドマークの検索可能。精度はGoogleに劣る。
13 国土地理院ジオコーディングAPI getPointsGSI 無料。日本国内の住所、ランドマークの検索可能。精度はGoogleに劣る。

pahooGeoCode.cpp

 677: /**
 678:  * ジオコーダAPI を用いて検索キーワードから緯度・経度を求める
 679:  *
 680:  * @param   wstring query 検索キーワード
 681:  *                         郵便番号表記 ###-####または#######(半角数字)
 682:  *                         緯度・経度表記 E##.##.##.#N###.##.##.#(半角数字)
 683:  *                         緯度・経度表記 ##.##,###.##(緯度,経度;半角数字)
 684:  * @param   string ua  UserAgent
 685:  * @param   int     api    0:APIを自動選定(省略時)
 686:  *                         1:Google Geocoding API
 687:  *                         2:Yahoo!ジオコーダAPI
 688:  *                        11:HeartRails Geo API
 689:  *                        12:OSM Nominatim Search API
 690:  *                        13:国土地理院ジオコーディングAPI
 691:  * @return  int ヒットした地点数
 692: */
 693: int pahooGeocode::searchPoints(wstring query, string ua, int api=0) {
 694:     static int apilist[] = { 1, 2, 11, 12, 13 };
 695:     int cnt;
 696:     wsmatch mt1;
 697:     // 郵便番号パターン
 698:     wregex re01(_SW("([0-9]{3})\\-?([0-9]{4})"));
 699:     // 緯度・経度パターン
 700:     wregex re11(_SW("(E|e|W|w|N|n|S|s)([0-9]+)\\.([0-9]+)\\.([0-9]+\\.?[0-9]*)(E|e|W|w|N|n|S|s)([0-9]+)\\.([0-9]+)\\.([0-9]+\\.?[0-9]*)"));
 701:     wregex re12(_SW("(E|e|W|w|N|n|S|s)([0-9]+\\.?[0-9]*)(E|e|W|w|N|n|S|s)([0-9]+\\.?[0-9]*)"));
 702:     wregex re13(_SW("(\\+|\\-)*([0-9]+\\.?[0-9]*)\\s*,\\s*(\\+|\\-)*([0-9]+\\.?[0-9]*)"));
 703: 
 704:     // 配列の初期化
 705:     for (int i = 0i < __SIZE_PPOINTSi++) {
 706:         this->Ppoints[i].longitude = 0;
 707:         this->Ppoints[i].latitude  = 0;
 708:         this->Ppoints[i].address   = L"";
 709:     }
 710: 
 711:     // 郵便番号かどうか
 712:     if (regex_search(query, mt1, re01)) {
 713:         cnt = this->getPointsHRG_postal(_WS(mt1[1].str() + mt1[2].str()), ua);
 714: 
 715:     // 緯度・経度表記かどうか
 716:     } else if (regex_search(query, mt1, re11)) {
 717:         cnt = 0;
 718:         string s1 = _WS(mt1[1].str());
 719:         string s2 = _WS(mt1[5].str());
 720:         double d1 = stod(mt1[2].str()) + stod(mt1[3].str()) / 60 + stod(mt1[4].str()) / (60 * 60);
 721:         double d2 = stod(mt1[6].str()) + stod(mt1[7].str()) / 60 + stod(mt1[8].str()) / (60 * 60);
 722:         if ((s1 == "E"|| (s1 == "e")) {
 723:             this->Ppoints[cnt].longitude = d1;
 724:         } else if ((s1 == "S"|| (s1 == "s")) {
 725:             this->Ppoints[cnt].longitude = -d1;
 726:         } else if ((s1 == "N"|| (s1 == "n")) {
 727:             this->Ppoints[cnt].latitude = d1;
 728:         } else if ((s1 == "S"|| (s1 == "s")) {
 729:             this->Ppoints[cnt].latitude = -d1;
 730:         }
 731:         if ((s2 == "E"|| (s2 == "e")) {
 732:             this->Ppoints[cnt].longitude = d2;
 733:         } else if ((s2 == "S"|| (s2 == "s")) {
 734:             this->Ppoints[cnt].longitude = -d2;
 735:         } else if ((s2 == "N"|| (s2 == "n")) {
 736:             this->Ppoints[cnt].latitude = d2;
 737:         } else if ((s2 == "S"|| (s2 == "s")) {
 738:             this->Ppoints[cnt].latitude = -d2;
 739:         }
 740:         cnt++;
 741:     } else if (regex_search(query, mt1, re12)) {
 742:         cnt = 0;
 743:         string s1 = _WS(mt1[1].str());
 744:         string s2 = _WS(mt1[3].str());
 745:         double d1 = stod(mt1[2].str());
 746:         double d2 = stod(mt1[4].str());
 747:         if ((s1 == "E"|| (s1 == "e")) {
 748:             this->Ppoints[cnt].longitude = d1;
 749:         } else if ((s1 == "S"|| (s1 == "s")) {
 750:             this->Ppoints[cnt].longitude = -d1;
 751:         } else if ((s1 == "N"|| (s1 == "n")) {
 752:             this->Ppoints[cnt].latitude = d1;
 753:         } else if ((s1 == "S"|| (s1 == "s")) {
 754:             this->Ppoints[cnt].latitude = -d1;
 755:         }
 756:         if ((s2 == "E"|| (s2 == "e")) {
 757:             this->Ppoints[cnt].longitude = d2;
 758:         } else if ((s2 == "S"|| (s2 == "s")) {
 759:             this->Ppoints[cnt].longitude = -d2;
 760:         } else if ((s2 == "N"|| (s2 == "n")) {
 761:             this->Ppoints[cnt].latitude = d2;
 762:         } else if ((s2 == "S"|| (s2 == "s")) {
 763:             this->Ppoints[cnt].latitude = -d2;
 764:         }
 765:         cnt++;
 766:     } else if (regex_search(query, mt1, re13)) {
 767:         cnt = 0;
 768:         string s1 = _WS(mt1[1].str());
 769:         string s2 = _WS(mt1[3].str());
 770:         double d1 = stod(mt1[2].str());
 771:         double d2 = stod(mt1[4].str());
 772:         if ((s1 == ""|| (s1 == "+")) {
 773:             this->Ppoints[cnt].latitude = d1;
 774:         } else if (s1 == "-") {
 775:             this->Ppoints[cnt].latitude = -d1;
 776:         }
 777:         if ((s2 == ""|| (s2 == "+")) {
 778:             this->Ppoints[cnt].longitude = d2;
 779:         } else if (s2 == "-") {
 780:             this->Ppoints[cnt].longitude = -d2;
 781:         }
 782:         cnt++;
 783: 
 784:     // APIを自動選定
 785:     } else if (api == 0) {
 786:         for (int api : apilist) {
 787:             this->resetError();
 788:             cnt = this->__searchPoints(query, ua, api);
 789:             if (cnt > 0)    break;
 790:         }
 791:     // API指定
 792:     } else {
 793:         cnt = this->__searchPoints(query, ua, api);
 794:     }
 795: 
 796:     return cnt;
 797: }

メソッド searchPoints は、前述の住所検索サービスを指定して呼び出すか、利用できるサービスを自動選定して呼び出す。
自動選定の順序は、上表にしたがう。GoogleAPIキーやYahoo!アプリケーションIDが未登録の場合は、これらの呼び出しをスキップする。また、そのクラウドサービスを利用することができなかったり、検索結果がゼロだった場合は、次点のサービスを自動的に呼び出す。
その他の関数、マップ描画、ヘルプファイルやインストーラー作成方法については、これまでの連載で説明してきたとおりである。

参考サイト

(この項おわり)
header