Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use google maps heatmap feature using angular agm

I am using angular component @agm/core at https://github.com/SebastianM/angular-google-maps for my latest angular 5 project to show google maps. It works great, and now I want to add google maps heatmap layer from visualization library. I can't figure out how to do it. please help

like image 844
Andri J Avatar asked Feb 22 '18 03:02

Andri J


1 Answers

The following steps worked for me (to note that I haven't found other examples for this after long search).

1. Install agm and googlemaps types:

npm install @agm/core --save
npm install @types/googlemaps --save-dev

2. Add "google" to types array inside compilerOptions in tsconfig. Eg:

"types": [
    "google"
]

3. When configuring agm module, append the visualization to your api key. Eg:

AgmCoreModule.forRoot({
    apiKey: '...' + '&libraries=visualization'
})

4. In your component html:

<agm-map [latitude]="..." [longitude]="..." (mapReady)="onMapLoad($event)">
</agm-map>

5. In your component ts:

private map: google.maps.Map = null;
private heatmap: google.maps.visualization.HeatmapLayer = null;

...

onMapLoad(mapInstance: google.maps.Map) {
    this.map = mapInstance;

    // here our in other method after you get the coords; but make sure map is loaded

    const coords: google.maps.LatLng[] = ...; // can also be a google.maps.MVCArray with LatLng[] inside    
    this.heatmap = new google.maps.visualization.HeatmapLayer({
        map: this.map,
        data: coords
    });
}

Make sure your map works fine first without the heatmap code (eg, set height, api key etc).

like image 79
André Mantas Avatar answered Sep 18 '22 23:09

André Mantas