angular - 使用 Angular 和 agm 获取用户地理位置并将 Google 热图居中
问题描述
我正在尝试呈现以当前用户位置为中心的 Google 热图。
我有点新手。我已经设置了基本方法,但我很难在正确的时间调用这些方法。我知道 Angular 中有各种生命周期钩子,但我认为这不是我想要的(?)。
我基本上想在渲染地图之前先获取用户的地理位置,然后将用户的位置作为地图的属性之一来渲染地图。
目前地图通过我的视图渲染:
<agm-map (mapReady)="onMapLoad($event)">
<agm-marker></agm-marker>
</agm-map>
但我想通过组件来做到这一点。所以函数调用是;第onGetLocatio()
一个回调到onShowPosition()
,然后 finallyonMapLoad()
必须被调用。这是正确的方法(调用顺序),还是有更好的方法?
这是我的控制器方法:
onGetLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.onShowPosition);
} else {
console.log('Not supported by browser!');
}
}
onShowPosition = (position: any) => {
this.currentLat = position.coords.latitude;
this.currentLong = position.coords.longitude;
this.currentLatLng = new google.maps.LatLng(this.currentLat, this.currentLong);
console.log(this.currentLatLng);
}
onMapLoad(mapInstance: google.maps.Map) {
this.map = mapInstance;
this.map.setCenter(this.currentLatLng);
this.heatmap = new google.maps.visualization.HeatmapLayer({
map: this.map,
data: this.heatMapData
});
}
先感谢您!
解决方案
推荐阅读
- android - 当我将目标 api 设置为 29 时未创建 Android 设备目录
- c - 在实施康威的生命游戏时发现错误时遇到问题。下面是源代码
- ios - 是否有任何方法可以处理数据库中的表情符号
- javascript - JavaScript:如何合并这两个不完整对象数组并制作一个完整对象数组
- java - 我们计算的请求签名与您提供的签名不匹配,甚至创建的签名
- dataset - “在 google colab 中增加你的内存”仍然可用吗?
- regex - 仅当使用 grep 本机正则表达式被两个正则表达式包围时如何提取正则表达式
- asp.net-core - 404 在 Blazor Web 程序集应用程序中找不到 blazor.webassembly.js
- javascript - 如何检查滚动是否低于某个级别?
- python-3.x - 用于解析的 Python 正则表达式