Power Appsで現在地を取得し、その現在地の住所を取得する方法を紹介します。この記事ではAPIを使わず、Power Appsだけで完結する方法を紹介します。Power Automateのプレミアムなどが契約できない場合など、できるだけ費用を抑えて実行する方法です。
目次 閉じる
住所データの準備
緯度・経度と住所情報が紐づいているデータとして、株式会社 Geoloniaが作成している(https://japanese-addresses-v2.geoloniamaps.com/api/ja.json)を利用します。
詳しい内容はこちら(https://github.com/geolonia/japanese-addresses-v2)を確認してください、
https://github.com/geolonia/japanese-addresses-v2 から取得したデータをPower Appsで使える形へと変換します。
Pythonで下記ライブラリを使います。事前にpip installしてください。
コピーしました!
pip install requests
pip install json
コピーしました!
import requests
import json
import os
# JSON取得
url = "https://japanese-addresses-v2.geoloniamaps.com/api/ja.json"
response = requests.get(url)
response.encoding = "utf-8"
data = response.json()
# 'data' の中を処理
collection_data = []
for pref_data in data["data"]: pref_name = pref_data.get("pref", "") pref_k = pref_data.get("pref_k", "") pref_r = pref_data.get("pref_r", "") pref_point = pref_data.get("point", []) # 緯度・経度 pref_lat, pref_lon = pref_point if len(pref_point) == 2 else (None, None) for city in pref_data.get("cities", []): city_name = city.get("city", "") city_ward = city.get("ward", "") city_point = city.get("point", []) # 緯度・経度 city_lat, city_lon = city_point if len(city_point) == 2 else (None, None) # データをコレクションに追加 collection_data.append( { "prefecture": pref_name, "pref_k": pref_k, "pref_r": pref_r, "city": city_name, "ward": city_ward, "prefecture_latitude": pref_lat, "prefecture_longitude": pref_lon, "city_latitude": city_lat, "city_longitude": city_lon, } )
# 結果確認(最初の10件)
print(json.dumps(collection_data[:10], ensure_ascii=False, indent=2))
# 出力ファイルのパスを.pyファイルと同じフォルダに設定
script_dir = os.path.dirname(os.path.abspath(__file__))
output_path = os.path.join(script_dir, "powerapps_clearcollect.txt")
# Power AppsのClearCollect形式で保存
with open(output_path, "w", encoding="utf-8") as f: f.write("ClearCollect(AddressCollection,\n") for item in collection_data: line = ( f' {{prefecture: "{item["prefecture"]}", city: "{item["city"]}", ward: "{item["ward"]}", ' f"prefecture_latitude: {item['prefecture_latitude']}, prefecture_longitude: {item['prefecture_longitude']}, " f"city_latitude: {item['city_latitude']}, city_longitude: {item['city_longitude']}}}" ) if item != collection_data[-1]: line += ",\n" else: line += "\n" f.write(line) f.write(");")
print(f"メモ帳に出力が完了しました!\n→ {output_path}")
こちらのコードでClearCollectで囲まれたデータが出力されます。Power Apps内に貼り付け利用できます。
Formulasに記述する場合には下のようにTableに囲って利用します。
コピーしました!
addressData=
Table( {prefecture: "北海道", city: "札幌市", ward: "中央区", prefecture_latitude: 141.347906782, prefecture_longitude: 43.0639406375, city_latitude: 141.35389, city_longitude: 43.061414}, {prefecture: "北海道", city: "札幌市", ward: "北区", prefecture_latitude: 141.347906782, prefecture_longitude: 43.0639406375, city_latitude: 141.340882, city_longitude: 43.090693},
,,,,
,,,,
,,,
);
Power Appsの実装
今回のアプリの完成イメージです。

Power Appsで現在地の緯度・経度を取得し、上で準備した住所データの中から近い住所を特定します。
Power AppsでLocation関数を使うことで、現在の緯度・経度を取得できます。ボタンのOnSelectなどに設定して取得します。
コピーしました!
Set(LocatiionDataIdo,Location.Latitude);
Set(LocatiionDataKeido,Location.Longitude);
この位置情報の取得はGPSだけでなくネットワークのIPアドレス等からもデータの取得を行います。そのため、GPSがOFFの端末などでは、正しくない位置情報が取得されることもあります。
緯度・経度を使った距離計算はHaversineの公式を使います。詳しくは省きますが、下記コードで計算できます。
コピーしました!
// 地球の半径 (Km)
Set(Radius, 6371);
// 現在地の緯度・経度
Set(currentLat, txt_ido2.Text);
Set(currentLon, txt_keido2.Text);
ClearCollect( nearestLocation, SortByColumns( AddColumns( addressData, Distance, 6371 * Atan( Sqrt( Power(Sin((Radians(addressData[@city_longitude]) - Radians(currentLat)) / 2), 2) + Cos(Radians(currentLat)) * Cos(Radians(addressData[@city_longitude])) * Power(Sin((Radians(addressData[@city_latitude]) - Radians(currentLon)) / 2), 2) ) ) ), "Distance",SortOrder.Ascending )
);
最寄りの住所は下記で取得できます。
コピーしました!
// 県 First(nearestLocation).prefecture
//市
First(nearestLocation).city
//区
First(nearestLocation).ward
アプリ・フローダウンロード
GitHubに公開しています。下記よりダウンロードしてご利用ください。