【发布时间】:2020-06-17 06:40:16
【问题描述】:
使用传单 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);
...
哪种方式正确?
第 2 块:
我重新制作了,如果我只有 1 组数据,它可以正常工作。 但是如果我有更多的 1 组,它会以错误的方式工作。 假设我有 2 个国家组,其中打开了页面上的任何国家/地区的一组位置(在运行 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 但是折线并没有像我预期的那样隐藏
点击所有国家 - 然后全部隐藏。 我在 BLOCK #2 中给出了代码描述:
谢谢!
【问题讨论】:
-
我不禁觉得您的代码......令人费解。有两个不同的地方
L.Polylines 正在被实例化,您似乎是在实例化L.Polylines 然后不将它们添加到任何L.LayerGroup中,但实际上您是将它们直接添加到地图中(这违背了拥有LayerGroups 的目的)。 -
感谢您的帮助。请看 BLOCK #2
-
请阅读第 3 块:
-
请阅读*.com/help/minimal-reproducible-example。 使其最小化,包括问题中的所有代码(编辑它以删除不需要的代码)
-
对不起,我不知道如何使它最小化。你的意思是我必须摆脱 BLOCK # 1-3 并将其作为 1 个问题?
标签: javascript leaflet