使用leaflet 1.6.0版本,我正在创建按国家分组的位置列表(标记和折线),需要在单击国家按钮时通过国家进行筛选。我使用layerGroup并使用以下方法成功显示标记和折线:
drawGroupedAdLocationsMarkers() {
let polylinePoints = [] // I get all info about all Polylines
let loop_index = 0
this.groupedAdLocations.forEach(nextGroupedAdLocations => { // draw all groupedAdLocations
this.groupedCountriesList[this.groupedCountriesList.length] = {
key: nextGroupedAdLocations.country,
label: this.countriesList[nextGroupedAdLocations.country],
}
let markersList = []
let polylinesList = []
nextGroupedAdLocations.adLocations.forEach(nextAddLocation => { // draw all nextAddLocation
let priorPoint = null // eslint-disable-line
let priorMarker = null // eslint-disable-line
if (loop_index > 0) {
priorPoint = this.groupedAdLocations[loop_index - 1]
priorMarker= nextMarker
}
polylinePoints[polylinePoints.length] = [nextAddLocation.lat, nextAddLocation.lng]
let nextMarker= this.showLocationMarker(nextAddLocation)
markersList[markersList.length] = nextMarker
polylinesList[polylinesList.length] = this.showLocationDirections(polylinePoints, nextGroupedAdLocations.country)
loop_index++
}) // nextGroupedAdLocations.adLocations.forEach(nextAddLocation => { // draw all nextAddLocation
polylinesList.map((nextPolyline) => {
markersList.push(nextPolyline);
});
let newMarkersLayerGroup = this.leaflet.layerGroup(markersList).addTo(this.locationsMap);
this.layerGroupsMarkersArray[this.layerGroupsMarkersArray.length] = {
country: nextGroupedAdLocations.country,
layersObj: newMarkersLayerGroup
}
}) // this.groupedAdLocations.forEach(nextGroupedAdLocations => { // draw all groupedAdLocations
let radius = 10;
let polyline = new this.leaflet.Polyline(polylinePoints, {
color: 'green',
opacity: 1,
weight: 2,
customData: {
type:'polyline'
// point_id: point.id,
// prior_point_id: priorPoint ? priorPoint.id : null,
},
offset: radius
});
polyline.on('click', function (event) {
event.stopPropagation();
window.event.cancelBubble = true;
// showModal(event)
// alert('Polyline clicked!');
});
// Add polyline to featuregroup
polyline.addTo(this.locationsMap);
}, // drawGroupedAdLocationsMarkers() {
创建标记/折线的方法:
showLocationMarker(nextAddLocation) {
let icon_size = 32
if (nextAddLocation.featured) {
icon_size = 48
}
var markerIcon = this.leaflet.icon({
iconUrl: (!nextAddLocation.featured ? '/images/location.png' : '/images/location_featured.png'),
iconSize: [icon_size, icon_size], // size of the icon
// shadowSize: [50, 64], // size of the shadow
iconAnchor: [icon_size, icon_size], // point of the icon which will correspond to marker's location
// shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [0, 0] // point from which the popup should open relative to the iconAnchor
});
let nextMarker = this.leaflet.marker(
[nextAddLocation.lat, nextAddLocation.lng],
{
icon: markerIcon,
customData:{add_location_id: nextAddLocation.id,type:'marker'}
})
.addTo(this.locationsMap)
.bindPopup(nextAddLocation.content)
.on('mouseover', this.locationMarkerOnMouseOver)
.on('click', this.locationMarkerOnClick)
.on('popupopen', this.locationMarkerOnPopupOpen)
// circleMarker
if (nextAddLocation.featured) {
nextMarker.bindTooltip("Featured Location").openTooltip();
}
let self = this
this.locationsMap.on('zoomend', function (/*e*/) {
self.current_zoom = self.locationsMap.getZoom()
});
if (nextAddLocation.opened) {
nextMarker.openPopup()
}
return nextMarker
}, // showLocationMarker(nextAddLocation) {
showLocationDirections(polylinePoints, country) {
let radius = 10;
let polyline = new this.leaflet.Polyline(polylinePoints, {
color: 'green',
opacity: 1,
weight: 2,
customData: {
type:'polyline'
// point_id: point.id,
// prior_point_id: priorPoint ? priorPoint.id : null,
},
offset: radius
});
// Add click listener
polyline.on('click', function (event) {
event.stopPropagation();
window.event.cancelBubble = true;
});
// Add polyline to featuregroup
polyline.addTo(this.locationsMap);
let decorator = this.leaflet.polylineDecorator(polyline, { // eslint-disable-line
patterns: [
// defines a pattern of 10px-wide dashes, repeated every 20px on the line
{
offset: 0,
repeat: 50,
symbol: this.leaflet.Symbol.arrowHead({
pixelSize: 10,
polygon: false,
pathOptions: {stroke: true}
})
}
]
}).addTo(this.locationsMap)
this.locationsMap.fitBounds(polyline.getBounds());
return polyline;
}, // showLocationDirections(polylinePoints) {
结果是我看到了一个有标记/折线的地图:https://prnt.sc/t1751f
点击筛选器中的方法可以隐藏/显示标记,但使用以下方法始终可见折线:
filterByGroupedCountry(country_key, country_label) {
let self = this
this.layerGroupsMarkersArray.forEach(nextLayerMarkerGroup => { // draw all layerGroupsMarkersArray
if (nextLayerMarkerGroup.country === country_key) {
let layersObj = nextLayerMarkerGroup.layersObj
if (self.locationsMap.hasLayer(layersObj)) {
self.locationsMap.removeLayer(layersObj);
} else {
self.locationsMap.addLayer(layersObj);
}
return
}
}) // this.layerGroupsMarkersArray.forEach(nextLayerMarkerGroup => { // draw all layerGroupsMarkersArray
我以上所写的把所有折线都推到标记数组中的方式是否不正确?
...
polylinesList.map((nextPolyline) => {
markersList.push(nextPolyline);
});
let newMarkersLayerGroup = this.leaflet.layerGroup(markersList).addTo(this.locationsMap);
...
哪种方式是正确的? BLOCK #2: 我进行了重构,如果只有1组数据,它可以正常工作。但是,如果我有多组数据,则无法按照正确的方式工作。 比如说,如果我在打开的页面上有两个国家组,并为每个国家设置了位置集合(在运行drawGroupedAdLocationsMarkers之后),那么我会看到标记和折线显示的很好。 当我点击隐藏第一个国家组(使用filterByGroupedCountry方法)时,只有标记被隐藏,但折线和装饰元素仍然可见。 当我点击隐藏第二(最后一个)国家组时,所有标记、折线和装饰元素都会被隐藏。 我认为,创建具有一个数组的LayerGroup的方式是错误的。
let newMarkersLayerGroup = this.leaflet.layerGroup(markersList);
如果我把所有的折线和装饰物都添加到标记列表(markersList)中,但哪种方法是有效的?
drawGroupedAdLocationsMarkers() {
let polylinePoints = [] // I get all info about all Polylines
let loop_index = 0
this.groupedCountriesList= []
this.groupedAdLocations.forEach(nextGroupedAdLocations => { // draw all groupedAdLocations
this.groupedCountriesList[this.groupedCountriesList.length] = { // keep list of countries for filtering countries list
key: nextGroupedAdLocations.country,
label: this.countriesList[nextGroupedAdLocations.country],
}
let markersList = []
let polylinesList = []
let decoratorsList = [] // init markers, polylines, decorators Lists withing one country group
nextGroupedAdLocations.adLocations.forEach(nextAdLocation => { // draw all adLocations inside of one country group
let priorPoint = null // eslint-disable-line
let priorMarker = null // eslint-disable-line
if (loop_index > 0) {
priorPoint = this.groupedAdLocations[loop_index - 1]
priorMarker= nextMarker
}
polylinePoints[polylinePoints.length] = [nextAdLocation.lat, nextAdLocation.lng]
// add new marker and add it to markersList
let nextMarker= this.showLocationMarker(nextAdLocation, nextGroupedAdLocations.country)
markersList[markersList.length] = nextMarker
let radius = 10; // Add new polyline based on point of nextAdLocation
let polyline = new this.leaflet.Polyline(polylinePoints, {
color: 'green',
opacity: 1,
weight: 2,
customData: {
add_location_id: nextAdLocation.id,
type:'polyline',
country:nextGroupedAdLocations.country
},
offset: radius
});
polyline.on('click', function (event) {
event.stopPropagation();
window.event.cancelBubble = true;
});
// polyline.addTo(this.locationsMap);
// add new decorator for polyline created above
let decorator = this.leaflet.polylineDecorator(polyline, { // eslint-disable-line
patterns: [
// defines a pattern of 10px-wide dashes, repeated every 20px on the line
{
offset: 0,
repeat: 50,
symbol: this.leaflet.Symbol.arrowHead({
pixelSize: 10,
polygon: false,
pathOptions: {stroke: true},
customData: {
add_location_id: nextAdLocation.id,
type:'polyline',
country:nextGroupedAdLocations.country
},
})
}
]
})
// decorator.addTo(this.locationsMap)
this.locationsMap.fitBounds(polyline.getBounds());
// add created polyline to polylinesList
polylinesList[polylinesList.length] = polyline
// add created decorator to decoratorsList
decoratorsList[decoratorsList.length] = decorator
loop_index++
}) // nextGroupedAdLocations.adLocations.forEach(nextAdLocation => { // draw all adLocations inside of one country group
polylinesList.map((nextPolyline) => {
markersList.push(nextPolyline);
});
decoratorsList.map((nextDecorator) => {
markersList.push(nextDecorator);
});
// create layer Group with polylinesList, markersList and decoratorsList
let newMarkersLayerGroup = this.leaflet.layerGroup(markersList);
this.locationsMap.addLayer(newMarkersLayerGroup);
this.layerGroupsMarkersArray[this.layerGroupsMarkersArray.length] = {
country: nextGroupedAdLocations.country,
layersObj: newMarkersLayerGroup
}
}) // this.groupedAdLocations.forEach(nextGroupedAdLocations => { // draw all groupedAdLocations
}, // drawGroupedAdLocationsMarkers() {
showLocationMarker(nextAdLocation, country) {
let icon_size = 32
if (nextAdLocation.featured) {
icon_size = 48
}
var markerIcon = this.leaflet.icon({
iconUrl: (!nextAdLocation.featured ? '/images/location.png' : '/images/location_featured.png'),
iconSize: [icon_size, icon_size], // size of the icon
// shadowSize: [50, 64], // size of the shadow
iconAnchor: [icon_size, icon_size], // point of the icon which will correspond to marker's location
// shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [0, 0] // point from which the popup should open relative to the iconAnchor
});
let nextMarker = this.leaflet.marker(
[nextAdLocation.lat, nextAdLocation.lng],
{
icon: markerIcon,
customData:{
add_location_id: nextAdLocation.id,
type:'marker',
country:country
}
})
.addTo(this.locationsMap)
.bindPopup(nextAdLocation.content)
.on('mouseover', this.locationMarkerOnMouseOver)
.on('click', this.locationMarkerOnClick)
.on('popupopen', this.locationMarkerOnPopupOpen)
if (nextAdLocation.featured) {
nextMarker.bindTooltip("Featured Location").openTooltip();
}
let self = this
this.locationsMap.on('zoomend', function (/*e*/) {
self.current_zoom = self.locationsMap.getZoom()
});
if (nextAdLocation.opened) {
nextMarker.openPopup()
}
return nextMarker
}, // showLocationMarker(nextAdLocation) {
filterByGroupedCountry(country_key, country_label) {
let self = this
this.layerGroupsMarkersArray.forEach(nextLayerMarkerGroup => { // draw all layerGroupsMarkersArray
if (nextLayerMarkerGroup.country === country_key) {
console.log('FOUND country_key::')
console.log(country_key)
let layersObj = nextLayerMarkerGroup.layersObj
console.log(0)
if (self.locationsMap.hasLayer(layersObj)) {
console.log(-1)
self.locationsMap.removeLayer(layersObj);
} else {
console.log(-2)
self.locationsMap.addLayer(layersObj);
}
return
}
}) // this.layerGroupsMarkersArray.forEach(nextLayerMarkerGroup => { // draw all layerGroupsMarkersArray
}
块 #3: 我制作了在线演示。 请打开http://ads.my-demo-apps.tk/login。 凭证已在输入框中。只需点击“登录”即可。 之后重定向到http://ads.my-demo-apps.tk/test2。 您必须看到地图上有一些点/折线和以下4个国家的清单。 尝试逐个单击国家。 您将看到相对标记被隐藏(或者再次显示,如果再次单击国家链接):https://prnt.sc/t8dsxb 但是折线并没有如我所期望的那样被隐藏。
单击所有国家-然后所有国家都会被隐藏。 我在块#2中给出了代码描述:
谢谢!
L.Polyline
,而且似乎你实例化了L.Polyline
但是又没有将它们添加到任何L.LayerGroup
中,而是直接添加到地图上(这违背了使用LayerGroup
的目的)。 - IvanSanchez