leaflet-search
leaflet-search copied to clipboard
Enhancement: search by more than one geojson attribute?
If possible, could you point me in the right direction for implementing this?
Thanks,
Mark
interesting! I can tell you that this thing can do it even now, simply by customizing two options: callData and filterJSON
callData you can completely customize the way you want to search in the attributes of each element filterJSON you can decide which is the condition for finding the element When you customize filterJSON you replace the default callback: https://github.com/stefanocudini/leaflet-search/blob/master/src/leaflet-search.js#L350
in this example you can understand to work: https://github.com/stefanocudini/leaflet-search/blob/master/examples/jsonp-filtered.html#L59
Is very complicated to explain in words. But I believe it will not be long time, I will add a new example that implements this feature, because I need it for an project. But now I do not have much time to work in Control.Search
please post your solution for other users
Hi Stefano, thanks for your reply! I had a look at this example before, but unfortunately I don't quite understand how it's meant..
I don't want to search for different geojson attributes, but in multiple geojson layers (for example in "markers1" and "markers2"). I tried it this way: var searchControl = new L.Control.Search({layer: markers1, markers2, propertyName: 'Name', circleLocation:true}); ..but that's apparently not the way it's supposed to be done. ; )
Could you please give me a short explanation of how else I could achieve this, in case it's not too complicated? Thanks so much!
simply... using callData you can select the recordsets to search.. then you can use callData callback to merge contents of two or more layers! or two or more fields. I do not have time now but maybe soon I will write a demo of this thing. Make changes to the current code by adding options for this issue not think it is constructive because can be it in more ways.. merging or intersect.. or sum markers in layer.. I thought as callData option for their such situations.
@marks maybe he was able to solve?
thanks again! I apologize, since I'm not a developer and just getting started with all this, I'm pretty slow in understanding all this despite trying my best ; ) I just tried to define my 2 layer variables in 1 variable like this, but I guess that's nonsense?
var allmarkers = { "markers A": markers1, "markers B": markers2 };
var searchControl = new L.Control.Search({layer: allmarkers, propertyName: 'Name', circleLocation:true}); …
Otherwise I think I'll just have to wait for your demo… : )
I admire your whish to learning, but I think the best way to lern and sole issue is to begin to read this page: http://leafletjs.com/reference.html It 's very clearly ...
Go forward with random code... is not a good idea, layer option only accept L.LayerGroup or L.GeoJSON objects ... not a literal Object {...}
for the demo you could wait a long long time ^_^
@stefanocudini @whitescale I never ended up figuring this out but I also didn't commit too much time to trying. I'll let you know if/when I get around to it and share my solution w/ the community!
Hi Stefano, thanks for your patience : ) I've already been through the leaflet basics, and actually do have a map and the search is working perfectly – all I was trying to do now is search in 2 sources (external geojson .js files), not just in 1. So my variables are in fact L.LayerGroups. I just thought there might be a very easy/obvious solution to searching in 2 layers, by writing something like "new L.Control.Search({layer: markers1, markers2, …" instead of just "layer: markers1".
"watch" this repo for updates..
will do :)
@marks thanks for your feedback as well!
I had this same issue/need and have yet to figure out an ideal solution. I tried passing it a L.LayerGroup of several marker clusters but the search fails spinning indefinitely. The workaround I implemented was to generate static data from all clusters and use callData. That partially works in that I get a list of items but autocompletion and autofiltering don't work, but they don't work in the "Static data" demo either so I figured it's by design.
@mjigmond the callData callback must to run callback parameter with an object like this:
var data = [
{title: "key 1", loc: [lat,lng] },
{title: "key 2", loc: [lat,lng] },
{title: "key 3", loc: [lat,lng] },
...
]
function localData(text, callResponse)
{
//here can use custom criteria or merge data from multiple layers
callResponse(data);
}
L.control.search({callData: localData});
...
keys.. is used in tooltip suggestions
That's the setup I used taken from the demo: http://labs.easyblog.it/maps/leaflet-search/examples/calldata.html
If you try the demo (Chrome on Linux&Windows at least) and type "wh" you would expect "white" to come up but there's no response.
yes it's a bug! I hope someone will fix it.. I don't have more free time at this period :(
Hello,
I'm sorry to re-open that discussion, I've been working on a leaflet project lately and I found your plugin really interesting. Great job! My question could be stupid but I couldn't figure out if you could change an already instanciated L.Control.Search? Like you do var searchControl = new L.Control.Search( ) and then searchControl.setLayer(layer). I've noticed this function exists in the source code but it doesn't seem to work, plus you've commented things linked to that function. Is it suppose to work? I didn't want to go too far in the code.
Thanks in advance for your answer.
It seems the https://opengeo.tech/maps/leaflet-search/examples/multiple-layers.html link is no longer working. Did someone happen to save it? The Wayback machine doesn't seem to have it and I'd like to find a worked example.
It seems the https://opengeo.tech/maps/leaflet-search/examples/multiple-layers.html link is no longer working. Did someone happen to save it? The Wayback machine doesn't seem to have it and I'd like to find a worked example.
now works