मैंने सुविधा की एक संपत्ति के आधार पर प्रत्येक सुविधा प्रकार को LayerGroup पर जोड़कर ऐसा किया। जैसे
GeoJSON
var data =[
{
type: "Feature",
properties: {
type: "type1"
},
geometry: {
type: "Point",
coordinates: [-1.252,52.107]
}
},
{
type: "Feature",
properties: {
type: "type2"
},
geometry: {
type: "Point",
coordinates: [-2.252,54.107]
}
}
];
GeoJSON लेयर बनाएं
//array to store layers for each feature type
var mapLayerGroups = [];
//draw GEOJSON - don't add the GEOJSON layer to the map here
L.geoJson(data, {onEachFeature: onEachFeature})//.addTo(map);
/*
*for all features create a layerGroup for each feature type and add the feature to the layerGroup
*/
function onEachFeature(feature, featureLayer) {
//does layerGroup already exist? if not create it and add to map
var lg = mapLayerGroups[feature.properties.type];
if (lg === undefined) {
lg = new L.layerGroup();
//add the layer to the map
lg.addTo(map);
//store layer
mapLayerGroups[feature.properties.type] = lg;
}
//add the feature to the layer
lg.addLayer(featureLayer);
}
तो फिर तुम पत्रक map.addLayer/removeLayer कार्यों जैसे कॉल कर सकते हैं
//Show layerGroup with feature of "type1"
showLayer("type1");
/*
* show/hide layerGroup
*/
function showLayer(id) {
var lg = mapLayerGroups[id];
map.addLayer(lg);
}
function hideLayer(id) {
var lg = mapLayerGroups[id];
map.removeLayer(lg);
}
धन्यवाद, परत माध्य addLayer/removeLayer को फिर से शुरू करता है? – dani
मुझे संदेह है कि आप 'प्रारंभिक (newgeojson, विकल्प)' भी कॉल कर सकते हैं, लेकिन मैंने इसे आजमाया नहीं है। हटाने और जोड़ने के लिए निश्चित रूप से काम करेंगे। – flup
असल में 'map.removeLayer (gj)' तो 'map.addLayer (gj)' काम नहीं करता है। –