Skip to content

Commit

Permalink
Merge pull request #34002 from dimagi/ze/add-mapbox-streets-layers
Browse files Browse the repository at this point in the history
Add Mapbox Streets Layers
  • Loading branch information
zandre-eng authored Jan 24, 2024
2 parents 3e5e386 + a665c41 commit 36cce77
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ hqDefine("geospatial/js/geospatial_map", [
};

function initMap() {
mapModel = new models.Map();
mapModel = new models.Map(false, true);
mapModel.initMap(MAP_CONTAINER_ID);

let selectedCases = ko.computed(function () {
Expand Down
92 changes: 90 additions & 2 deletions corehq/apps/geospatial/static/geospatial/js/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ hqDefine('geospatial/js/models', [
};
};

var Map = function (usesClusters) {
var Map = function (usesClusters, usesStreetsLayers) {
var self = this;

self.usesClusters = usesClusters;
self.usesStreetsLayers = usesStreetsLayers;

self.mapInstance;
self.drawControls;
Expand Down Expand Up @@ -132,10 +133,97 @@ hqDefine('geospatial/js/models', [
if (self.usesClusters) {
createClusterLayers();
}

if (self.usesStreetsLayers) {
loadMapBoxStreetsLayers();
addLayersToPanel();
}
};

function loadMapBoxStreetsLayers() {
self.mapInstance.on('load', () => {
self.mapInstance.addSource('mapbox-streets', {
type: 'vector',
url: 'mapbox://mapbox.mapbox-streets-v8',
});

self.mapInstance.addLayer({
id: 'landuse_overlay',
source: 'mapbox-streets',
'source-layer': 'landuse_overlay',
type: 'line',
paint: {
'line-color': '#800080', // purple
},
layout: {
'visibility': 'none',
},
});
self.mapInstance.addLayer({
id: 'road',
source: 'mapbox-streets',
'source-layer': 'road',
type: 'line',
paint: {
'line-color': '#000000', // black
},
'layout': {
'visibility': 'none',
},
});
self.mapInstance.addLayer({
id: 'admin',
source: 'mapbox-streets',
'source-layer': 'admin',
type: 'line',
paint: {
'line-color': '#800080', // purple
},
'layout': {
'visibility': 'none',
},
});
});
}

function addLayersToPanel() {
self.mapInstance.on('idle', () => {
const toggleableLayerIds = ['landuse_overlay', 'admin', 'road'];
const menuElement = document.getElementById('layer-toggle-menu');
for (const layerId of toggleableLayerIds) {
// Skip if layer doesn't exist or button is already present
if (!self.mapInstance.getLayer(layerId) || document.getElementById(layerId)) {
continue;
}

const link = document.createElement('a');
link.id = layerId;
link.role = 'button';
link.href = '#';
link.textContent = layerId;
link.className = 'btn btn-secondary';
link.onclick = function (e) {
const clickedLayer = this.textContent;
e.preventDefault();
e.stopPropagation();

const visibility = self.mapInstance.getLayoutProperty(clickedLayer, 'visibility');
if (visibility === 'visible') {
self.mapInstance.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.classList.remove('active');
} else {
this.classList.add('active');
self.mapInstance.setLayoutProperty(clickedLayer, 'visibility', 'visible');
}
};

menuElement.appendChild(link);
}
menuElement.classList.remove('hidden');
});
}

function createClusterLayers() {
// const mapInstance = self.mapInstance;
self.mapInstance.on('load', () => {
self.mapInstance.addSource('caseWithGPS', {
type: 'geojson',
Expand Down
13 changes: 13 additions & 0 deletions corehq/apps/geospatial/templates/base_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
{{ block.super }}
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.css">
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.0/mapbox-gl-draw.css" type="text/css">
<style>
#layer-toggle-menu {
background: #fff;
position: absolute;
z-index: 1;
top: 10px;
left: 10px;
max-height: 150px;
border: 1px solid rgba(0, 0, 0, 0.4);
overflow-y: auto;
}
</style>

{% endblock %}


Expand Down
8 changes: 7 additions & 1 deletion corehq/apps/geospatial/templates/map_visualization.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@
Previous disbursement was cleared.
{% endblocktrans %}
</div>
<div id="geospatial-map" style="height: 500px"></div>
<div id="geospatial-map" style="height: 500px">
<div id="layer-toggle-menu" class="btn-group-vertical hidden">
<h4 class="text-center">
{% trans 'Layers' %}
</h4>
</div>
</div>

<!-- For Pagination -->
<div class="panel-body-datatable">
Expand Down

0 comments on commit 36cce77

Please sign in to comment.