0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Google places apiを使い倒す

Posted at

今日やること
google places apiの使い方を理解して、電話番号から店舗情報を取得する

Google places api

まず事前準備がいくつかあります。

最初にGoogleアカウントが必要ですので、アカウントがない場合は作成しておきます。 また、 Google Places API Web Service の仕様を確認してみると、「APIキー」なるものが必要とありますのでこちらも取得します。 APIキーを取得する手順は以下の通りです。

1.Google API Console でログインする。

2. Google APIの認証情報へいく

スクリーンショット 2021-03-06 17.56.25.png

3. 認証情報を作成をクリックしAPIを作成する

スクリーンショット 2021-03-06 17.57.09.png

4. APIを保存する

無事にAPIが作成されたのでどこかに保存しておきましょう。

google places search rest api

google places search rest apiを読んで電話番号で位置情報を取得したい。

記事を確認したが現時点では日本語の記事が少なくドキュメントを理解する方が早そう、、、

今回使っていくのはFind placeという機能です。

これにより名称や電話番号を使ってピンポイントに施設を検索することができるようです。

URLに情報を追加していく

先程のドキュメントからHTTPのフォームを取得することができるのでそこに情報を追加していく。

https://maps.googleapis.com/maps/api/place/findplacefromtext/output?parameters

今回はjson形式で情報を取得したいのでoutputjsonに変更。Required parametersを見るとkey, input, inputtypeをそれぞれ指定する必要があるので必要な情報を指定していく。

今回の場合はこのようになった。

https://maps.googleapis.com/maps/api/place/findplacefromtext/json?&key=mykeycode&input=%2B810426349723&inputtype=phonenumber&fields=name(etc.)

inputの[%2B]とは[+]のことだがURLエンドコードで利用する場合このように変換しておかないとエラーになる。

より詳しい情報を取得したい場合はPlace Detailsを利用する。

この場合必要になる情報は下記の通りだ。

・output / json or xml
・key / api key
・place_id / Place Searchで取得できるもの
オプションとして次のものを指定できる
・language / 選択可
・fields / 選択可

では先ほどと同様にURLに必要な情報を入力していこう

https://maps.googleapis.com/maps/api/place/details/json?&key=mykeycode&place_id=ChIJR0OkRtgdGWAR2rkr1KG8dqA&fields=formatted_phone_number

これでおそらく電話番号を取得できると思う。他にもいろいろな情報を取得できるので遊んでみよう!

今日はここまで。

今日の課題

Postモデルに保存した写真をサーバー上でダウンロード

Gmapmapを使って地図上に緯度と軽度でpostを表示する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?