From acda2ca2d69c219a9c7c66a55b4498bed636fe40 Mon Sep 17 00:00:00 2001 From: Laura Eckman Date: Thu, 3 Oct 2019 11:40:38 -0700 Subject: [PATCH] make clusters clickable hurricane-response/response-theme#22 --- _layouts/home.html | 32 +++++++++++++++++++++++++------- _layouts/pod.html | 34 +++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/_layouts/home.html b/_layouts/home.html index 1988f9c..812588c 100644 --- a/_layouts/home.html +++ b/_layouts/home.html @@ -12,7 +12,7 @@ >
{% include header.html %} - +
{% include txtbot.html %}
@@ -23,7 +23,7 @@

Shelter Locations

- +

Legend

@@ -50,7 +50,7 @@

Legend

Shelter

-
+ @@ -61,7 +61,7 @@

Legend

//TODO: move this to config. const MAP_LOAD_CENTER = [{{ site.shelter_map_center }}]; - const ZOOM = [{{ site.shelter_zoom }}]; + const INITIAL_ZOOM = [{{ site.shelter_zoom }}]; var map = new mapboxgl.Map({ // container id specified in the HTML @@ -70,8 +70,7 @@

Legend

style: 'mapbox://styles/miklb-c4tb/cjnozdzs41qlx2rqvf7mwfztt', // initial position in [lon, lat] format center: MAP_LOAD_CENTER, - // initial zoom - zoom: ZOOM + zoom: INITIAL_ZOOM }); @@ -128,6 +127,25 @@

Legend

"text-size": 12 } }); + + // on mouseover, make it obvious that clusters are interactive + map.on('mouseenter', 'clusters', () => { + map.getCanvas().style.cursor = 'pointer'; + }); + map.on('mouseleave', 'clusters', () => { + map.getCanvas().style.cursor = ''; + }); + + map.on('click', 'clusters', e => { + const cluster = map.queryRenderedFeatures(e.point, { layers: ['clusters'] })[0]; + const clusterId = cluster.properties.cluster_id; + const center = cluster.geometry.coordinates; + map.getSource('shelters').getClusterExpansionZoom(clusterId, (err, zoom) => { + err || map.easeTo({ center, zoom }); + }); + }); + + map.loadImage("{{site.baseurl}}/assets/images/shelter-icon.png", (error, img) => { map.addImage("shelter", img); map.addLayer({ @@ -152,7 +170,7 @@

Legend

map.on('mouseenter', 'unclustered-point', function () { map.getCanvas().style.cursor = 'pointer'; }); - + // Change it back to a pointer when it leaves. map.on('mouseleave', 'unclustered-point', function () { map.getCanvas().style.cursor = ''; diff --git a/_layouts/pod.html b/_layouts/pod.html index b843897..256e9d2 100644 --- a/_layouts/pod.html +++ b/_layouts/pod.html @@ -12,7 +12,7 @@ >
{% include header.html %} - +
{% include txtbot.html %}
@@ -22,7 +22,7 @@

Points of Distribution and Food Locations

- +

Legend

@@ -49,14 +49,14 @@

Legend

Shelter

-
+