1

I added a plugin for searching a GeoJSON file in leaflet, but now there are multiple search buttons and they do not recognize any input. I suspect that it is initializing for every single instance of a feature within the GeoJSON file. How do I change my script to only initialize once? (continued from this question: Activating Search Plugin with JS Leaflet enter image description here

 // load GeoJSON from an external file
$.getJSON("Syriashape.json", function (data) {
// add GeoJSON layer and popups to the map once the file is loaded
var syriaLayer = L.geoJson(data, {
 style: function (feature) {
 return {color: 'red', weight: '1pt'};
 },
 onEachFeature: function (feature, layer) {
 layer.bindPopup(
 "Sheet Number: " + feature.properties.Sheet_Numb + "</br>" + 
 "Catalog Record: " + "<a href = '" + feature.properties.LOC_Catalo + "'>" + feature.properties.LOC_Catalo + "</a>");
 //activate search button
 var searchControl = new L.Control.Search({layer: syriaLayer, propertyName: 'Sheet_Numb', circleLocation:false});
 searchControl.on('search_locationfound', function(e) {
 e.layer.setStyle({});
 if(e.layer._popup)
 e.layer.openPopup();
 }).on('search_collapsed', function(e) {
 featuresLayer.eachLayer(function(layer) { //restore feature color
 featuresLayer.resetStyle(layer);
 }); 
 });
 map.addControl( searchControl ); //inizialize search control
 }
}).addTo(map);
});
asked Oct 7, 2015 at 17:49
2
  • Do you really mean to add a search control for every returned geoJSON feature? Commented Oct 7, 2015 at 19:15
  • In this case, there will only be one feature, so I just need a single search control for that one layer Commented Oct 7, 2015 at 20:20

1 Answer 1

0

To check if the onEachFeature-function is called more than one time you could use an alert:

...
onEachFeature: function (feature, layer) {
 alert(feature.id);
...

But in general I think there is no need to put the creation of the search control into the onEachFeature-Loop. Just put it at the end of the ajax callback function:

 // load GeoJSON from an external file
 $.getJSON("Syriashape.json", function(data) {
 // add GeoJSON layer and popups to the map once the file is loaded
 var syriaLayer = L.geoJson(data, {
 style: function(feature) {
 return {
 color: 'red',
 weight: '1pt'
 };
 },
 onEachFeature: function(feature, layer) {
 layer.bindPopup(
 "Sheet Number: " + feature.properties.Sheet_Numb + "</br>" +
 "Catalog Record: " + "<a href = '" + feature.properties.LOC_Catalo + "'>" + feature.properties.LOC_Catalo + "</a>");
 }
 }).addTo(map);
 // When Layer is added...add Search Control ... activate search button
 var searchControl = new L.Control.Search({
 layer: syriaLayer,
 propertyName: 'Sheet_Numb',
 circleLocation: false
 });
 searchControl.on('search_locationfound', function(e) {
 e.layer.setStyle({});
 if (e.layer._popup)
 e.layer.openPopup();
 }).on('search_collapsed', function(e) {
 featuresLayer.eachLayer(function(layer) { //restore feature color
 featuresLayer.resetStyle(layer);
 });
 });
 map.addControl(searchControl); //inizialize search control
 });
answered Oct 8, 2015 at 7:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.