Angular-google-maps: 显示交通层

创建于 2018-05-16  ·  4评论  ·  资料来源: SebastianM/angular-google-maps

如何在 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

所有4条评论

可以通过底层 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);
}

希望能帮助到你!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Subhojit1992 picture Subhojit1992  ·  3评论

nthonymiller picture nthonymiller  ·  4评论

ostapch picture ostapch  ·  4评论

dineshkumar20 picture dineshkumar20  ·  3评论

n1t3w0lf picture n1t3w0lf  ·  3评论