私はangular2 mapsで遊んでいましたが、最後にはgoogle maps apiを直接使いたいと思います。Google Maps APIをangular2に読み込むにはどうすればよいですか?
これを角度2にロードするには、どのような方法が最適ですか?現在私はそれをindex.htmlファイルに挿入されたスクリプトタグで読み込んでいます。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Ourlatitude</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
</head>
<body>
<script
src="https://maps.googleapis.com/maps/api/js?key=my_api_key">
</script>
<app-root>Loading...</app-root>
</body>
</html>
それからマップを保持する成分のngAfterViewInit()フック内部コンポーネントにマップを作成します。
ngAfterViewInit() {
this.map = new google.maps.Map(this.mymap.nativeElement, {zoom: 4, center: {lat: -25.363, lng: 131.044} });
}
これはうまくいくようですが、スクリプトタグを追加するのは正しいことではありません。私はangular-cli.jsonスクリプトにスクリプトを追加しようとしました:[]配列、それは動作していないようです(new google.maps.Map()
への呼び出しはgoogleが未定義で失敗します)。
私はnpm install --save @types/google-maps
でタイプをインストールしましたが、認識されているようです。
私は実際にangular2-google-mapsの使い方を知っています。これは私が現在使っているものですが、私は生のAPIを直接、特に非同期でロードし、スクリプトタグをインデックスに追加することはしませんでした。コンポーネント内のコードにスクリプトを追加するなど、いくつかのアプローチがありますが、ほとんどがちょっとハッキリしているようですので、角度2のこのapiを読み込む正しい方法は何かを尋ねています。 – nPn