AGM MAPangular2でトラフィックレイヤーを表示するにはどうすればよいですか。
基になるgoogle.maps.mapオブジェクトを介して追加することで可能です。 実際のGoogleマップオブジェクトを取得するには、 mapReady
イベントを使用する必要があります。 次に、それを処理するコードで、トラフィックレイヤーを追加できます。 mapReadyの「イベント」は、実際のgooglemapsインスタンスです。
HTML
<agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>
TypeScript
onMapReady(mapInstance) {
let trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(mapInstance);
}
Typescriptエラーを回避するには、Googleマップのタイプ定義をインポートする必要があります。
import {} from '@types/googlemaps';
次の方法でインストールできます。
npm install --save @types/googlemaps
素晴らしい! ziftytoddに感謝します。
私の場合、 google
オブジェクトはangular-cliベースのプロジェクトで定義されていなかったため、 window.google.maps.TrafficLayer()
を明示的に参照する必要がありました。
@ziftytodd解決策をありがとうございます。 本当に助かりました。
私は他の人にも役立つかもしれないいくつかの調整をしなければなりませんでしたが。
1- npm install --save @types/googlemaps
2-メインパスにindex.d.tsファイルを作成し、次のように記述します: declare module 'googlemaps';
3-タイプスクリプトをインポートします: import {} from 'googlemaps';
4-HTMLファイル内: <agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>
5-タイプスクリプト内:
onMapReady($event) {
let trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap($event);
}
それが役に立てば幸い!
最も参考になるコメント
基になるgoogle.maps.mapオブジェクトを介して追加することで可能です。 実際のGoogleマップオブジェクトを取得するには、
mapReady
イベントを使用する必要があります。 次に、それを処理するコードで、トラフィックレイヤーを追加できます。 mapReadyの「イベント」は、実際のgooglemapsインスタンスです。HTML
<agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>
TypeScript
Typescriptエラーを回避するには、Googleマップのタイプ定義をインポートする必要があります。
import {} from '@types/googlemaps';
次の方法でインストールできます。
npm install --save @types/googlemaps