leaflet-search icon indicating copy to clipboard operation
leaflet-search copied to clipboard

Enhancement: search by more than one geojson attribute?

Open marks opened this issue 10 years ago • 18 comments

If possible, could you point me in the right direction for implementing this?

Thanks,

Mark

marks avatar Jan 13 '14 18:01 marks

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

stefanocudini avatar Jan 14 '14 17:01 stefanocudini

please post your solution for other users

stefanocudini avatar Mar 16 '14 02:03 stefanocudini

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!

whitescale avatar Apr 15 '14 15:04 whitescale

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?

stefanocudini avatar Apr 15 '14 16:04 stefanocudini

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… : )

whitescale avatar Apr 15 '14 19:04 whitescale

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 avatar Apr 15 '14 20:04 stefanocudini

@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!

marks avatar Apr 16 '14 00:04 marks

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".

whitescale avatar Apr 16 '14 09:04 whitescale

"watch" this repo for updates..

stefanocudini avatar Apr 16 '14 17:04 stefanocudini

will do :)

whitescale avatar Apr 16 '14 18:04 whitescale

@marks thanks for your feedback as well!

whitescale avatar Apr 17 '14 08:04 whitescale

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 avatar Jun 10 '14 18:06 mjigmond

@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

stefanocudini avatar Jun 10 '14 18:06 stefanocudini

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.

mjigmond avatar Jun 11 '14 01:06 mjigmond

yes it's a bug! I hope someone will fix it.. I don't have more free time at this period :(

stefanocudini avatar Jun 11 '14 23:06 stefanocudini

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.

miliare avatar Jul 30 '14 09:07 miliare

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.

craigmaloney avatar Apr 01 '22 18:04 craigmaloney

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

stefanocudini avatar Apr 01 '22 19:04 stefanocudini