- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 asymmetrik's ngx-leaflet and ngx-leaflet-markercluster具有以下版本:
"@asymmetrik/ngx-leaflet": "^5.0.2",
"@asymmetrik/ngx-leaflet-markercluster": "^2.1.1"
在开发过程中一切正常。但是,当我构建用于生产的 Angular 项目 (ng serve --prod
) 时,我无法看到集群标记,并且在 Chrome 的控制台中出现以下错误:
ERROR TypeError: a.markerClusterGroup is not a function
at t.ngOnInit (main-es2015.94505399b2d83c23de95.js:1)
at main-es2015.94505399b2d83c23de95.js:1
at main-es2015.94505399b2d83c23de95.js:1
at Kb (main-es2015.94505399b2d83c23de95.js:1)
at xw (main-es2015.94505399b2d83c23de95.js:1)
at Object.updateDirectives (14-es2015.a3c30690716486447af1.js:1)
at Object.updateDirectives (main-es2015.94505399b2d83c23de95.js:1)
at Xb (main-es2015.94505399b2d83c23de95.js:1)
at rw (main-es2015.94505399b2d83c23de95.js:1)
at nw (main-es2015.94505399b2d83c23de95.js:1)
我正在我的组件中导入“leaflet”和“leaflet.markercluster”。下面是 map.component.ts 代码:
import { Component, Input, SimpleChanges } from '@angular/core';
import { tileLayer, latLng, control, marker, divIcon, LatLngBounds, MarkerClusterGroup, MarkerClusterGroupOptions } from 'leaflet';
import { NgElement, WithProperties } from '@angular/elements';
import * as L from 'leaflet';
import 'leaflet.markercluster';
...
@Component({
selector: 'map',
styleUrls: ['./map.component.scss'],
templateUrl: './map.component.html'
})
export class MapComponent {
layers = [];
markerClusterGroup: L.MarkerClusterGroup;
markerClusterData: any[] = [];
maxBounds: L.LatLngBounds;
map: L.Map;
options = {
layers: [
tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18 }),
],
zoom: 2,
zoomControl: false,
maxZoom: 18
};
markerClusterOptions: L.MarkerClusterGroupOptions = {
showCoverageOnHover: true,
animate: true,
chunkedLoading: true,
zoomToBoundsOnClick: true,
spiderfyOnMaxZoom: true,
chunkDelay: 500,
animateAddingMarkers: true,
chunkInterval: 7,
iconCreateFunction: function (cluster) {
var markers = cluster.getAllChildMarkers();
var icon = divIcon({
iconSize: [35, 35],
iconAnchor: [10, 10],
popupAnchor: [10, 0],
shadowSize: [0, 0],
html: '<div>' + cluster.getChildCount() + '</div>'
});
if (markers.find(x => x.options.alt == enum.A)) {
icon.options.className = 'cluster cluster-critical';
} else if (markers.find(x => x.options.alt == enum.B)) {
icon.options.className = 'cluster cluster-warning';
} else {
icon.options.className = 'cluster cluster-normal';
}
return icon;
}
};
ngOnChanges(changes: SimpleChanges): void {
this.fixMap();
}
mapReady(map: L.Map) {
this.map = map;
map.addControl(control.zoom({ position: 'bottomright' }));
this.fixMap();
}
fixMap() {
if (this.map) {
this.setBounds();
this.map.fitBounds(this.maxBounds);
this.initMarkers();
setTimeout(() => {
this.map.invalidateSize();
}, 0);
}
}
markerClusterReady(group: L.MarkerClusterGroup) {
this.markerClusterGroup = group;
this.markerClusterGroup.addLayers(this.markerClusterData);
}
initMarkers() {
this.markerClusterData = [];
this.objectList.forEach(x => {
var location = latLng(x.location.latitude, x.location.longitude);
var system = marker(location, {
icon: divIcon({
iconSize: [30, 30],
iconAnchor: [10, 10],
popupAnchor: [10, 0],
shadowSize: [0, 0],
className: this.getMarkerStyle(x),
}),
title: x.serialNumber,
alt: x.objectStatusSummary.status
});
system.bindPopup(fl => this.createPopupComponent(x), {
className: 'popup',
closeButton: false
}).on('click', (data) => {
this.focusOnMarker(x);
});
this.markerClusterData.push(system);
});
}
focusOnMarker(object: Object) {
var bounds = new L.LatLngBounds([[object.objectLocation.latitude, object.objectLocation.longitude]]);
this.map.fitBounds(bounds, {
animate: true,
duration: 1,
easeLinearity: 1,
maxZoom: this.map.getZoom(),
noMoveStart: true
});
}
setBounds() {
var latLngArray: any[] = [];
if (this.objectList && this.objectList.length > 0) {
this.objectList.forEach(x => {
latLngArray.push({
lat: x.objectLocation.latitude,
lng: x.objectLocation.longitude,
});
});
}
else {
latLngArray.push({
lat: 0,
lng: 0
});
}
this.maxBounds = new L.LatLngBounds(latLngArray);
}
getMarkerStyle(object: object) {
return 'marker marker-' + this.businessHelper.getClassByStatus(<objectHealthStatusEnum>object.objectStatusSummary.status) + ' marker-' + object.serialNumber;
}
resetZoom() {
this.map.fitBounds(this.maxBounds, {
animate: true,
duration: 1,
easeLinearity: 1,
noMoveStart: true
});
}
public createPopupComponent(object) {
const popupEl: NgElement & WithProperties<MapPopupComponent> = document.createElement('popup-element') as any;
popupEl.addEventListener('closed', () => document.body.removeChild(popupEl));
popupEl.object = object;
document.body.appendChild(popupEl);
return popupEl;
}
}
这是map.component.html代码:
<div class="map-wrapper">
<div
leaflet
[leafletOptions]="options"
(leafletMapReady)="mapReady($event)"
[leafletMarkerCluster]="markerClusterData"
[leafletMarkerClusterOptions]="markerClusterOptions"
(leafletMarkerClusterReady)="markerClusterReady($event)"
></div>
</div>
可以找到主要代码here .
有什么想法吗?
最佳答案
我遇到了同样的问题并找到了解决方案。问题是有不同的传单从定义中导入为“L”。确保首先导入传单,然后是其他包,例如:
import * as L from 'leaflet';
import { Map, MapOptions, MarkerClusterGroup, MarkerClusterGroupOptions } from 'leaflet';
import 'leaflet.markercluster';
另一种猜测是生产时间与开发时间不同。您的 initMarkers
通过 ngOnChanges
被调用,但这可能为时过早。映射或集群可能尚未初始化。尝试在 markerClusterReady
或 mapReady
initMarkers
关于angular - 错误类型错误 : a. markerClusterGroup 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847492/
我目前正在使用 asymmetrik's ngx-leaflet and ngx-leaflet-markercluster具有以下版本: "@asymmetrik/ngx-leaflet": "^5
我想在 map 缩小时为标记集群下的标记打开一个弹出窗口。当用户点击搜索结果时调用此函数。 这是我正在使用的代码: map.eachLayer(function (layer) { if (l
我正在尝试使用 markerclustergroups 和多边形来显示集群。现在显示了多边形,但没有显示集群。我一直在尝试为多边形使用质心,因为 markerclustergroup 似乎不喜欢多边形
在传单中有一个扩展名为:Leaflet.markercluster在此扩展中,有一个名为markerClusterGroup的函数,它可以聚类并返回多个标记(点)的计数。参见示例:Here . 在我的
我有两个偶尔重叠的独立 markerClusterGroup。有什么办法可以防止这种情况发生吗?在我的实际代码中,我为其中一个集群组使用自定义图标,以便我可以区分这两种集群类型。但是,对于此示例而言,
我正在尝试将标记簇添加到传单中。 var markers = L.markerClusterGroup(); 我的头文件包括: script(src='https://unpkg.com/lea
我试过 Mapbox 和他们的 API 来创建交互式 map 。目的是获取 geojson 文件中的点,并将它们显示在 map 上。它们必须通过标记图标进行过滤,并根据应用的缩放比例进行分组。 我在使
我尝试使用 Leaflet 的 Cluster Group 插件,但我不断收到“Uncaught TypeError: L.markerClusterGroup is not a function”
我是一名优秀的程序员,十分优秀!