
サンプル・プログラム
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.cpp | APIキーの管理(ソース) |
sour/apikey.hpp | APIキーの管理(ヘッダ) |
sour/makefile | ビルド |
バージョン | 更新日 | 内容 |
---|---|---|
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 | 使用ライブラリ更新 |
バージョン | 更新日 | 内容 |
---|---|---|
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()メソッド追加 |
バージョン | 更新日 | 内容 |
---|---|---|
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() 追加 |
使用ライブラリ
リソースの準備
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;
解説:クラス
今回は、クラウドサービスにアクセする処理を 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
値 | サービス名 | メソッド | 制 約 |
---|---|---|---|
1 | 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 = 0; i < __SIZE_PPOINTS; i++) {
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: }
自動選定の順序は、上表にしたがう。GoogleAPIキーやYahoo!アプリケーションIDが未登録の場合は、これらの呼び出しをスキップする。また、そのクラウドサービスを利用することができなかったり、検索結果がゼロだった場合は、次点のサービスを自動的に呼び出す。
参考サイト
- PHPで住所・ランドマークから緯度・経度を求める:ぱふぅ家のホームページ
- PHPでクラスを使ってテキストの読みやすさを調べる:ぱふぅ家のホームページ
- WiX によるWindowsインストーラー作成:ぱふぅ家のホームページ
- C++ 開発環境の準備:ぱふぅ家のホームページ
- デジタルスチルカメラ用画像ファイルフォーマット規格 Exif 2.3:カメラ映像機器工業会規格
「PHPで住所・ランドマークから緯度・経度を求める」で作ったPHPプログラムをC++に移植したものである。
(2025年3月16日)ネット接続チェック強化,使用ライブラリ更新
(2024年11月30日)使用ライブラリ更新
(2024年8月17日)使用ライブラリ更新
(2024年3月23日)使用ライブラリ更新