UGA Boxxx

つぶやきの延長のつもりで、知ったこと思ったこと書いてます

【Google Map API】ランドマークの検索をする

Google Map API を使ってランドマーク検索を行いたい
そこで、Places API を使用する

developers.google.com

やりたいこと

入力したテキストを送信し、場所名と緯度経度を受け取りたい
APIへのリクエストはサーバー(node.js)から行う

使い方

エンドポイントは以下

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

必須パラメーター

  • key: アプリケーションの APIキー
  • input: テキスト入力された検索キーワード
  • inputtype: 入力のタイプ
    textquery またはphonenumberのいずれかであるが、textqueryを使用する

オプションのパラメーター

  • language: 結果が返される言語を示す言語コード
  • fields: 返すデータのフィールドをコンマで区切って指定する
    今回は場所名と緯度経度が欲しいのでname,geometryを指定する
  • locationbias: 半径と緯度/経度を指定するか、長方形の点を表す緯度/経度のペアを2つ指定することにより、指定した領域で結果を優先させる
    今回は特に指定しない

node.jsから呼び出す

公式のライブラリがあるのでそれを使う github.com

import {Client, Status} from "@googlemaps/google-maps-services-js";
const client = new Client({});

client
  .findPlaceFromText({
    params: {
      key: config.agreed.config.googlePlaceApi.key,
      language,
      input,
      inputtype: "textquery",
      fields: "name,geometry",
    },
  })
  .then((r) => {
    if (r.data.status === Status.OK) {
      console.log(r.data.results[0].elevation);
    } else {
      console.log(r.data.error_message);
    }
  })
  .catch((e) => {
    console.log(e);
  });

価格

月間使用量(1,000 回あたりの料金)
- 0~100,000 $17.00 - 100,001〜500,000 $13.60

ちょっとお高め