There are two modules providing lat/lon storage in Drupal 8/8: geolocation and geofield. I went with geofield simply because geocluster is using it. geocluster clusters on the server side and that lets you display an astonishing amount of elements on a single map. While geolocation is mostly a single module, there’s an entire family of modules we will need here. Also, as far as I can tell, we are limited to leaflet here because I can’t find ready made geojson support for anything else.
composer require drupal/geocluster drupal/views_geojson drupal/leaflet drupal/leaflet_geojson
- Add a geofield (NOT a geocluster — that’s a bug, a patch has been filed), I called it coordinates
- Add a view, no page, no block, nothing.
- leave the pager in place. We will remove it later.
- Add field Geocluster lat (coordinates). Leave aggregator settings on “Group results together”.
- Add field Geocluster lon (coordinates)
- Add field Geocluster result count (coordinates)
- Add field coordinates and exclude it from display
- Add title and exclude it from display. Set aggregator settings to “GROUP_CONCAT”.
- Remove sort criteria.
- Now add a GeoJSON export and in settings click Enable geocluster for this search.
- Set Map Data Sources to Other: Lat/Lon Point
- Set Latitute field to Geocluster lat (coordinates)
- Set Longitude field to Geocluster lon (coordinates)
- Set Title field to Title.
- Now magic has happened! If you set up Views to show the query, you will see
GROUP BY node__coordinates_coordinates_geocluster_index_1is the only
GROUP BYleft. This is why it works at all.
- You can now remove the pager.
- Add a Views GeoJSON: Bounding box contextual filter
- Provide default value
- Type: Query Parameter
- Query parameter: bbox
- Fallback value: -180,-90,180,90
- The leaflet_geojson module provides a block which miraculously (well, based on the GeoJSON export) will pick up up our views and it’ll just work.