Angular-google-maps: Show traffic layer

Created on 16 May 2018  ·  4Comments  ·  Source: SebastianM/angular-google-maps

How can I show traffic layer in AGM MAP angular2.

Most helpful comment

It's possible by adding it via the underlying google.maps.map object. To get the actual Google Maps object, you need to use the mapReady event. Then in the code that handles that, you can add the traffic layer. The "event" of the mapReady is the actual google maps instance.

HTML
<agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>

TypeScript

onMapReady(mapInstance) {
    let trafficLayer = new google.maps.TrafficLayer();
    trafficLayer.setMap(mapInstance);
}

You'll need to import the Google Maps types definition to avoid any Typescript errors:
import {} from '@types/googlemaps';
Which you can install via:
npm install --save @types/googlemaps

All 4 comments

It's possible by adding it via the underlying google.maps.map object. To get the actual Google Maps object, you need to use the mapReady event. Then in the code that handles that, you can add the traffic layer. The "event" of the mapReady is the actual google maps instance.

HTML
<agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>

TypeScript

onMapReady(mapInstance) {
    let trafficLayer = new google.maps.TrafficLayer();
    trafficLayer.setMap(mapInstance);
}

You'll need to import the Google Maps types definition to avoid any Typescript errors:
import {} from '@types/googlemaps';
Which you can install via:
npm install --save @types/googlemaps

Super! Thanks ziftytodd.

For me the google object was undefined in my angular-cli based project, so I had to explicitly reference to window.google.maps.TrafficLayer()

@ziftytodd Thank you for your solution. It was really helpful for me.

Although I had to make some adjustments that may also be helpful to others.

1 - npm install --save @types/googlemaps
2 - create an index.d.ts file in the main path and write: declare module 'googlemaps';
3 - import in your typescript: import {} from 'googlemaps';
4 - in your HTML file: <agm-map (mapReady)="onMapReady($event)" ... plus any other attributes ...> ... </agm-map>
5 - in your typescript:

onMapReady($event) {
let trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap($event);
}

Hope it helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nthonymiller picture nthonymiller  ·  4Comments

Halynsky picture Halynsky  ·  3Comments

ostapch picture ostapch  ·  4Comments

gizm0bill picture gizm0bill  ·  4Comments

supran2811 picture supran2811  ·  4Comments