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

标签 angular google-maps-api-3

我正在使用 Angular 组件@agm/core https://github.com/SebastianM/angular-google-maps我最新的 Angular 5 项目显示谷歌地图。它工作得很好,现在我想从可视化库添加谷歌地图热图图层。我不知道该怎么做。请帮忙

最佳答案

以下步骤对我有用(注意,经过长时间搜索后我还没有找到其他示例)。

1. 安装 agm 和 googlemaps 类型:

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

2. 将“google”添加到 tsconfig.json 中的 compilerOptions 内的类型数组中。例如:

"types": [
    "google"
]

3. 配置 agm 模块时,将可视化附加到您的 api key 。例如:

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

4. 在你的组件 html 中:

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

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
    });
}

首先确保您的 map 在没有热图代码(例如设置高度、API key 等)的情况下正常工作。

关于angular - 如何使用 Angular agm 来使用谷歌地图热图功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919237/

相关文章:

Angular 和 p5.js - p5.loadSound 不是函数

angular - 确定 Angular 支持的 Typescript 版本

python - 为什么 Shapely 无法检测给定的地理坐标点是否在 route

google-maps - 谷歌地图在网络应用程序中离线

javascript - 使用谷歌地图无法通过邮政编码在 map 上获取正确的位置

javascript - Angular2 组件样式/css 未在 ngAfterViewInit 中被浏览器应用

html - 在产品构建中找不到 Font Awesome 相对文件路径

javascript - 负载下拉值按字母顺序排列 Angular 6

javascript - 如何从 google map api 中的 json 文件中读取 json 数据

ios - NSJSONSerialization解析数据问题