【Power Apps】現在地の住所を取得する方法(Google Geocoding API)

【Power Apps】現在地の住所を取得する方法(Google Geocoding API)

Power Appsで現在地を取得し、その現在地の住所を取得する方法を紹介します。

Google Maps Geocoding API の有効化と API キー取得

今回のアプリはGoogle Cloud Platform(GCP)のGoogle Geocoding APIを利用します。

詳細は公式のドキュメントに沿ってAPIを取得してください。

Power Apps・Power Automateの実装

アプリ画面

今回のアプリの完成イメージです。

Power Appsで現在地の緯度・経度を取得し、Power AutomateでHTTPアクションでAPIを実行し、住所を取得する方法で現在地の住所を取得します。

現在地の取得

Power AppsでLocation関数を使うことで、現在の緯度・経度を取得できます。ボタンのOnSelectなどに設定して取得します。

コピーしました!

Power Apps
Set(LocatiionDataIdo,Location.Latitude);
Set(LocatiionDataKeido,Location.Longitude);

この位置情報の取得はGPSだけでなくネットワークのIPアドレス等からもデータの取得を行います。そのため、GPSがOFFの端末などでは、正しくない位置情報が取得されることもあります。

API用のURIを作成する

Google Geocoding APIをPower Automateで実行してデータを取得しますが、Power AutomateのHTTPアクションで使うURIの値をPower Apps側ですべて作成します。

Google Geocoding APIで緯度・経度から住所を取得するには下記のURIの形式にする必要があります。これをPower Apps側で作成し、Power Automateに渡します。

コピーしました!

Google Geocoding API URI
https://maps.googleapis.com/maps/api/geocode/json?latlng={緯度},{経度}&key={API_KEY}

緯度・経度はSet変数でグローバル変数に格納しましたが、下ではテキストボックスの値を取得するようにしています。デバックで色んな緯度・経度で試す際に手入力できると便利でした。

コピーしました!

Power Apps
//Google Geocoding APIキーを入力
Set(ApiKey,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

//Power AutomateのHTTPアクションのURIを設定
Set(apiUri, "https://maps.googleapis.com/maps/api/geocode/json?latlng=" 
& txt_ido1.Text & "," & txt_keido1.Text & "&key=" & ApiKey);

Power Automateで住所を取得する

今回作成するフロー全体図はこちら。

Power AppsからHTTP実行用のURIを取得し、データを加工してPower Appsに返すフローです。途中でデータを加工していますが、APIで取得するデータが多すぎるため、必要そうな部分のみに削っています。

Power Appsからデータを受け取るためにPower Apps (V2)で、下記のように取得する値を設定します。今回はPower Appsで作成したURIを受け取るための値として「apiUrl」を設定します。

HTTPアクションでURIにPower Appsから取得した値を設定します。

HTTPで取得した値をJSON の解析で使えるようにします。

コンテンツ
@{body('HTTP')?['results']}  

スキーマ
{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "address_components": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "long_name": {
                            "type": "string"
                        },
                        "short_name": {
                            "type": "string"
                        },
                        "types": {
                            "type": "array",
                            "items": {
                                "type": "string"
                            }
                        }
                    },
                    "required": [
                        "long_name",
                        "short_name",
                        "types"
                    ]
                }
            },
            "formatted_address": {
                "type": "string"
            },
            "geometry": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "object",
                        "properties": {
                            "lat": {
                                "type": "number"
                            },
                            "lng": {
                                "type": "number"
                            }
                        }
                    },
                    "location_type": {
                        "type": "string"
                    },
                    "viewport": {
                        "type": "object",
                        "properties": {
                            "northeast": {
                                "type": "object",
                                "properties": {
                                    "lat": {
                                        "type": "number"
                                    },
                                    "lng": {
                                        "type": "number"
                                    }
                                }
                            },
                            "southwest": {
                                "type": "object",
                                "properties": {
                                    "lat": {
                                        "type": "number"
                                    },
                                    "lng": {
                                        "type": "number"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "navigation_points": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "object",
                            "properties": {
                                "latitude": {
                                    "type": "number"
                                },
                                "longitude": {
                                    "type": "number"
                                }
                            }
                        }
                    },
                    "required": [
                        "location"
                    ]
                }
            },
            "place_id": {
                "type": "string"
            },
            "plus_code": {
                "type": "object",
                "properties": {
                    "compound_code": {
                        "type": "string"
                    },
                    "global_code": {
                        "type": "string"
                    }
                }
            },
            "types": {
                "type": "array",
                "items": {
                    "type": "string"
                }
            }
        },
        "required": [
            "address_components",
            "formatted_address",
            "geometry",
            "place_id",
            "types"
        ]
    }
}

選択アクションで必要なデータに絞ります。

開始
@{body('JSON_の解析')}

マップ
{
  "long_name": @{item()['address_components']?[0]?['long_name']},
  "short_name": @{item()['address_components']?[0]?['short_name']},
  "type": @{join(item()['types'],',')}
}

選択アクションで取得したデータを文字列として変数に格納し、Power Appsへ返します。

Power Appsでフローを実行する

Power Appsでフローを実行し、Jsonをテーブルに変換します。コピーしました!

Power Apps
//フロー実行
    UpdateContext({_GoogleGeoodingApiResult:GoogleGeoodingApi.Run(apiUri).apiresponsebody});
    //実行結果をテーブルへ変換
    UpdateContext(
           {
               GoogleGeoodingApiResult: ForAll(
                   Table(ParseJSON(_GoogleGeoodingApiResult)),
                   {
                    long_name:Text(ThisRecord.Value.long_name),
                    short_name:Text(ThisRecord.Value.short_name),
                    type:Text(ThisRecord.Value.type)
                   }
                   )
            }
            );
    


取得したデータはGoogleGeoodingApiResultに格納しています。

アプリ・フローダウンロード

GitHubに公開しています。下記よりダウンロードしてご利用ください。

参考記事など

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA