angular - Angular 上的 Mapbox GL JS MarkerClusterGroup
问题描述
我正在使用 MapBox GL 和 Leaflet 库在 Ionic / Angular 中制作应用程序。无论如何,我碰巧在实现 L.MarkerClusterGroup () 函数时遇到了问题。目前我使用一个函数从firebase加载每个标记的数据,这工作正常,但将图层添加到地图以将每个标记添加到 MarkerClustergroup 对象。
这是我用来实例化地图的函数
getCurrentCoordinates() {
this.geolocation.getCurrentPosition().then((resp) => {
this.latitude = resp.coords.latitude;
this.longitude = resp.coords.longitude;
this.map = new mapboxgl.Map({
container: 'mapBox',
style: 'mapbox://styles/cherniakovsky/ckn3idmad27xu17r1fb13a6rx',
center: [this.longitude, this.latitude],
zoom: 16.15,
maxZoom: 18,
cluster: true,
clusterMaxZoom: 14,
clusterRadius: 50
});
}).catch((error) => {
console.log('Error getting location', error);
});
}
这工作正常,然后我用下一个函数加载标记
loadPins() {
for (const rec of this.chatRooms) {
const icons: Record<string, { icon: string }> = {
rec: {
icon: rec.img.filepath,
},
};
let el = window.document.createElement('div');
el.id = 'avatar';
el.style.backgroundImage = 'url(' + rec.img.filepath + ')';
el.style.width = '50px';
el.style.height = '50px';
el.style.borderRadius = '50%';
el.style.backgroundSize = '50px';
el.draggable = true;
const popup = new mapboxgl.Popup({ offset: 25 }).setHTML(this.definePopup(rec));
const latlngM = L.latLng(rec.location.longitude, rec.location.latitude);
this.marcadores =
new mapboxgl.Marker(el).setLngLat({lon: rec.location.longitude, lat: rec.location.latitude }).setPopup(popup).on('popupopen', e => {
this.elementRef.nativeElement.querySelector('.chat-link')
.addEventListener('click', () => this.openChat(rec));
}).addTo(this.map);
this.clusters.addLayer(L.marker(latlngM));
this.map.addLayer(this.clusters);
}
}
最后,该函数加载标记但不将它们聚集在一起。变量 this.clusters 是瞬时的,如下所示:
clusters = new L.MarkerClusterGroup({
polygonOptions: {
fillColor: '#3887be',
color: '#3887be',
weight: 2,
opacity: 1,
fillOpacity: 0.5
}
});
我在互联网示例中看了很多,但是当我尝试将图层添加到地图时,会收到很多未定义图层的错误消息。我不明白为什么。感谢阅读
解决方案
推荐阅读
- php - Isset 检查 Array - Blade Laravel 5.8
- slider - 如何摆脱 Vuetify 滑块中的消息区域?
- azure - 来自 Salesforce 的 Azure 数据工厂管道,在接收器映射中添加时间戳
- javascript - 事件侦听器不会触发 JavaScript 上动态创建的元素
- spring - 使用 Docker 部署 Spring 和 MySQL 应用程序失败
- c# - Blazor 服务器端应用程序 api 请求问题 httpClient.PostAsync IHttpClientFactory
- graphql - Jooq 中的 SortField 不允许对字段进行排序
- docker - 在 Dockerfile 中的单个 RUN 指令中指定多个 UNIX 命令的目的
- sharepoint-2010 - 计算 Infopath 字段中的项目
- git - GitKraken中的Githook预提交错误退出代码1