1
1

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 1 year has passed since last update.

vue+typescript+js-api-loaderでのgooglemap使用方法【'google' is not defined】

Last updated at Posted at 2021-11-16

色々ハマったのでメモ。

型定義パッケージを追加してtsconfig.jsonのtypesに追加

terminal
npm i -D @types/google.maps 
tsconfig.json
"compilerOptions": {
//////省略//////////
    "types": [
      "google.maps", //追加
    ],
//////省略//////////
}

#Loaderをインストール

terminal
npm install @googlemaps/js-api-loader 
import { Loader } from "@googlemaps/js-api-loader"

ここまでは公式通り

#トラブルシューティング('google' is not defined)
const google=window.google;を追加

Map.vue
  mounted() {
    const loader = new Loader({
      apiKey: "YOUR_API_KEY",
      version: "weekly",
    });

    let map: google.maps.Map;

    loader.load().then(() => {
      const google=window.google; //追加
      map = new google.maps.Map(document.getElementById("map") as HTMLElement, {
        center: {lat: -34.397, lng: 150.644},
        zoom: 8
      });
    })
  },
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?