I originally posted this in the Views GeoJson module issue queue, but the more I think about it, it seems like it would be more likely a core OL question.

Basically, I have a view outputting a GeoJson feed, I'm enabling that feed as a map overlay, then I'm displaying it in a separate view.

The problem comes up when I expose filters. Exposing filters on the map view doesn't change the geojson data and therefor the map is not updated. Exposing filters on the geojson view (I'm outputting it as a block) actually filters the content correctly but it takes me to the raw json output and doesn't display things on the map.

While I thought at first this was a problem with views geojson, that view is filtering 100% correctly. The problem is how the map is displayed and reacts to a change in the feed data.

Am I thinking of this the wrong way? I feel like I'm missing something simple but I'm definitely hitting a wall. Any help anyone can give me is much appreciated.

Comments

m.stenta’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

Cleaning up old issues. Views GeoJSON and Openlayers have changed drastically since this was posted. If you're still having this issue with the latest versions, please reopen!

jlsevillano’s picture

I followed this post to create a map with layer: http://www.petejwhite.com/drupal-views-with-openlayers-3-0-beta1/

But when i tried to make a filter to select some data with, for example Country, not runs...always display all items

GeoJSON 7.x.1.0-beta3+2-dev
Openlayers 7.x-3.1

Any solution?

Thanks

ekes’s picture

Status: Closed (works as designed) » Active

Also #2663964: How to make a map with exposed filters?.

And I'll add my use case we've got FacetAPI queries to add to the url, not views ones.

But in all cases you are calling the Views GeoJSON path which is set in the GeoJSON class options. It doesn't have any interaction with the URL (or specifically the $_GET) options of the page the map being displayed to user is on.

I've looked to see where I could inject the $_GET vars from the page into the request made in the OpenLayers JS, but there doesn't seem a point before the JS is made. It could be done in the Javascript['settings'], but that's really ugly. It's that or replace some of the js that openlayer supplies.

BUT I may well, actually probably have, overlooked a better way.

ekes’s picture

Title: Exposed filters break when using Views GeoJson module » How to pass exposed filters or other $_GET variables to OL map js querying GeoJSON

Maybe better description.

ekes’s picture

So I can confirm the very hackish solution can work:-

/**
 * Implements hook_js_alter().
 *
 * Hack to set the path of the GeoJSON url to include the facets.
 *
 * Here the _GET parameters for 'f' (used by Facet API) and 'search_api_views_fulltext' used by
 * views filter supplied by search api views module are passed onto the MODULE-MAP
 * (which has a path /map).
 *
 * To do the same you'd have to correct for the $map_key and the $_GET params that your
 * exposed filters or whatever need, and the path of the geojson view. Ugly, but best I came up
 * with for now.
 */
function MODULE_js_alter(&$javascript) {
  if (empty($_GET['f']) && empty($_GET['search_api_views_fulltext'])) {
    return;
  }
  foreach ($javascript['settings']['data'] as $delta => $settings) {
    if (is_array($settings)
      && !empty($settings['openlayers']['maps'])
      && ($map_key = key($settings['openlayers']['maps']))
      && substr($map_key, 0, 23) == 'openlayers-map-MODULE-MAP'
    ) {
      foreach ($settings['openlayers']['maps'][$map_key]['source'] as $source_key => $source_settings) {
        if (!empty($source_settings['opt']['url']) && $source_settings['opt']['url'] == '/map') {
          $query = array();
          if (!empty($_GET['f'])) {
            $query['f'] = $_GET['f'];
          }
          if (!empty($_GET['search_api_views_fulltext'])) {
            $query['search_api_views_fulltext'] = $_GET['search_api_views_fulltext'];
          }
          $javascript['settings']['data'][$delta]['openlayers']['maps'][$map_key]['source'][$source_key]['opt']['url'] = '/map?' . drupal_http_build_query($query);
        }
      }
    }
  }
}
jlsevillano’s picture

I'm going to try it, Thanks for all.

m.stenta’s picture