如何在 AGM MAP angular2 中显示流量层。
可以通过底层 google.maps.map 对象添加它。 要获取实际的 Google Maps 对象,您需要使用mapReady
事件。 然后在处理该问题的代码中,您可以添加流量层。 mapReady 的“事件”是实际的谷歌地图实例。
HTML
<agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>
打字稿
onMapReady(mapInstance) {
let trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(mapInstance);
}
您需要导入 Google Maps 类型定义以避免任何 Typescript 错误:
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 Maps 对象,您需要使用
mapReady
事件。 然后在处理该问题的代码中,您可以添加流量层。 mapReady 的“事件”是实际的谷歌地图实例。HTML
<agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>
打字稿
您需要导入 Google Maps 类型定义以避免任何 Typescript 错误:
import {} from '@types/googlemaps';
您可以通过以下方式安装:
npm install --save @types/googlemaps