简体   繁体   English

如何使用 angular agm 使用谷歌地图热图功能

[英]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.我在https://github.com/SebastianM/angular-google-maps 上使用 angular 组件@agm/core用于我最新的 angular 5 项目来显示谷歌地图。 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请帮忙

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: 1. 安装 agm 和 googlemaps 类型:

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

2. Add "google" to types array inside compilerOptions in tsconfig. 2. 在 tsconfig 的 compilerOptions 中的 types 数组中添加“google”。 Eg:例如:

"types": [
    "google"
]

3. When configuring agm module, append the visualization to your api key. 3. 配置 agm 模块时,将可视化附加到您的 api 密钥。 Eg:例如:

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

4. In your component html: 4. 在你的组件 html 中:

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

5. In your component ts: 5. 在你的组件 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).首先确保您的地图在没有热图代码的情况下正常工作(例如,设置高度、api 键等)。

Instead of appending your api key, use the libraries key for AgmCoreModule inside app.module.ts to import the visualization library as so:不要附加您的 api 密钥,而是使用 app.module.ts 中的 AgmCoreModule 的库密钥来导入可视化库,如下所示:

AgmCoreModule.forRoot({
  apiKey: environment.googleMapsAPIKey,
  libraries: ['visualization'],
}),

If you see something like:如果你看到类似的东西:

@types/googlemaps/index.d.ts is not a module

just create a file called index.d.ts directly within your src directory and add the following line:只需直接在 src 目录中创建一个名为 index.d.ts 的文件并添加以下行:

declare module 'googlemaps';

Thank you André Mantas for your solution谢谢安德烈·曼塔斯的解决方案

I faced a number of issues related to the 'google' types.我遇到了许多与“谷歌”类型相关的问题。 So, I added this line to the component controller所以,我将此行添加到组件控制器

declare var google: any;

private map: any;
private heatmap: any;

and it worked它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM