Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello, I have a geojson feed going into an Openlayers map that is created by views. In the geojson view, I am exposing a filter, outputting it as a block, and displaying that block on the end result map page.
The default value of the filter is passed to the map, however when I update the filter to a value different than the default, the page is redirected to the text-based json output instead of changing the content inside of the map.
So basically my question is: how can I get an exposed filter to work with geojson and openlayers in views?
Thanks in advance for any help that anyone can provide.
Comments
Comment #1
basvredelingIt seems asynchronously loaded view_geojson layers do not inherit the exposed filter values. That includes full text search filters.
What happens is this:
I implemented a workaround for the "search" exposed filter by altering the map definition like so:
Replace search by your exposed filter id. And replace MYMODULE / GEOJSON_LAYER_ID too.
I don't like this solution. There should be an openlayers native solution which passes exposed filters to geojson layers. This solution also assumes that you have the same exposed filter id's configured on all geojson feed displays as well as on the openlayers map display itself.
Comment #2
bendev CreditAttribution: bendev commentedHello,
here is how I proceeded with OPL v3 (thanks to Pol)
It works fine excepted for the handling of the 'All' value which doesn't work.
if I go to the geojson feed url and i remove the filter of the url (=ALL) . I get all results.
I tried with filter=ALL, filter=& and without filter...
not working so far
Comment #3
droces CreditAttribution: droces commentedBendev's code worked for me, solving the OL Map respecting exposed filters, but with two modifications:
GEOJSON_LAYER_ID
needed to be myGEOJSON_SOURCE_ID
and…
$context -> options['url'] .= $param;
Needed to be:
$context -> setOption('url', $context -> getOption('url') . $param);
However, I was still getting a page of code when I used the exposed filters. That was because the exposed filters form's #action was pointing back to that feed's url. So basically I needed a hook_form_alter to post the exposed filters form to the current page:
Comment #4
jlsevillano CreditAttribution: jlsevillano commentedI quite confuse where can i do it (patch).
My GeoJSON view is: tramos-json
My Page with exposed filter: tramos-mapa
Have i to add, where, which module?
Thanks
Comment #5
jlsevillano CreditAttribution: jlsevillano commentedI created a custom module, but when i filter:
Notice: Undefined property: Drupal\openlayers\Plugin\Style\Circle\Circle::$machine_name in line 5
if ($context->machine_name == 'GEOJSON_SOURCE_ID') {
what's wrong?
Comment #6
robertwb CreditAttribution: robertwb commentedI think that this is more appropriately an OL question / there is a similar thread for Leaflet which supplies a patch to enable additional filters to be used when accessing Views GeoJSON data sources. Changing project -- apologies if I am misreading this.
Comment #7
basvredelingComment #8
jesus.soto CreditAttribution: jesus.soto commentedI have recently working using exposed filters on ol3 geojson views. I hope I can help you.
1- First at all, create a Geojson view:
You need create an OpenLayer data view, add filtering criteria fields and finally over the view add a geojson feed
Once you have created the view, test it using the url directly.
/geojsonv2 (without parameters)
/geojsonv2/param1/param2 (using parameters)
Important: The administrative identifier of each filter must be the same as in the view that contains the exposed form.
2- Get parameters from exposed form:
You must build an url that includes the parameters selected in the exposed form. Example:
3- Ajax Request and Update
Finally, launch the ajax request using the url that includes the parameters from the exposed form.
When the information is received in geojson format, load all features in the specific source of the map.
Ajax changes in an exposed form should update the map if the view is linked to a source.
It would be great if this type of functionality were incorporated natively on this great drupal module.