LoginSignup
9

More than 5 years have passed since last update.

GoogleMaps APIKey対応

Posted at

状況

2018-07-16 から、 Google Maps API を呼ぶのに API key が必須になります。
ずっと以前から運用しているレガシーなサイトで Google Maps JS API (v3) を API key なしで使っていたので対応する必要がありました。

まず API key が必須になる要件ですが、単に Google Maps をサイト内に埋め込んでいる場合などは対象ではありません。
Google オフィシャルの Chrome 拡張 (Google Maps Platform API Checker - Chrome ウェブストア) があるので、これを入れて確かめてみましょう。

google_maps_api_checker_info.png

こんなふうに表示されれば、埋め込みで使っているだけなので対応は不要です。

google_maps_api_checker_error.png

このようにエラーが表示されれば対応が必要です。

References

要件

API key の対応にはクレジットカードの登録が求められるなど、小規模なサイト運用者にはハードルが高い点も。
不安を取り除くため、課金条件を確認しておきましょう。

課金に関する話ですので、実際のサイトの本文を、以下に一部引用しておきます。 (Google のサイトでは日本語訳もちゃんと用意されています)

  • Starting July 16, 2018, a new pay-as-you-go pricing plan will go into effect for Maps, Routes, and Places. This new plan gives you more flexibility and control over how you use our APIs: You can use as much or as little as you need and only pay for what you use each month. We would also like to highlight that beginning July 16th, we’re changing the pricing for our Maps, Routes, and Places products.
  • Starting July 16, 2018, when you enable billing, you get $200 free usage every month for Maps, Routes, or Places. Based on the millions of users using our APIs today, most of them can continue to use Google Maps Platform for free with this credit.
  • You only pay for what you use. You can review rates and access your spending any time in your Google Cloud Platform Console, where you can also set daily quotas to protect against unexpected increases. You can also set billing alerts to receive email notifications when charges reach a preset threshold determined by you.
  • In addition to the $200 monthly free credit, all users get:
    • Free Maps usage for iOS, Android, and Embed (for displaying maps only)
    • Free Maps URLs
  • Even though the first $200 a month is free, we ask for your credit card or billing account to cover any amount you spend over this free credit.

僕の英語力が正しければ、要約すると 2018-07-16 からクレジットカード登録が必要な API Key 取得が必要になるけど、毎月 $200 の無料使用枠が付いてくるから、大抵の場合、今までどおり無料で使えるはずだよ。超過が心配な場合は GCP コンソールで使用制限の設定をしておいてね、ということ。

課金要件に引っかかっても私の方では保証できないので、実際にサイトを見て確かめておいてください。

実際の利用量に対する課金金額は Pricing Calculator で確認できます。

References

対応

それでは実際の対応です。

まず こちらのページ から API key の取得を開始してください。

  1. 使用する機能を選択します (Maps / Routes / Places)
  2. 使用する Google API のプロジェクトを選択します (プロジェクト未作成の場合は作成してください)
  3. Google の billing account を登録します (クレカの登録が必要です)

API key が作成されたら、 こちら で作成された key を選択し、 Key restrictions の設定を必ず行ってください。

対象のプロジェクトを選択したら、 [API keys] のところに作成した API key があると思うので、これの名前のところをクリックして、次の画面で Key restrictions のところを編集します。

今回のように JS API で使うということであれば

  • Application restrictions で "HTTP referrers (web sites)" を選び https://hogehoge.com/* のように使いたいページの URL を指定
  • API restrictions で "Maps JavaScript API" を選択

google_maps_api_key_restriction.png

これをやっておかないと、 API key は外部にさらすことになるので、悪用されかねません。

つづいて、 Quotas (使用量制限) の設定をします。この制限を超過すると、マップは表示されないようになりますが、 $200 の範囲内に収めておけば課金はされなくなります。もちろん、超過しそうな場合で、課金に値するし、課金する余裕もあると思ったら、制限値は緩めておいてくださいね!

GCP console で対象プロジェクトを選択して、デイリーでの map load 上限値などを設定します。

google_maps_api_quotas.png

最後に API key を API コール時に渡すようにします。
script タグで読み込んでいる場合は、 key パラメータで API key を渡します。

<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"
        async defer></script>

JS の Google API Client Library を使用して load している場合は、次のように渡します。

google.load("maps", "3", {
  "other_params" : "libraries=weather&sensor=false&key=API_KEY"
});
google.setOnLoadCallback(function() {
  //...
})

設定反映後、サイトにアクセスして、このように API checker の表示が SUCCESS に変われば OK です!

google_maps_api_checker_success.png

References

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
9