Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Map Grid to support multiple maps #892

Merged
merged 21 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"esversion": 6
"esversion": 8
}
4 changes: 3 additions & 1 deletion cartoframes/assets/init.js.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ document
const show_info = '{{show_info}}' === 'True';
const layers = {{ layers|tojson }};
const bounds = {{ bounds }};
const camera = null;
const camera = {{ camera|tojson }};
const mapboxtoken = '{{mapboxtoken}}';
const basemap = '{{basemap}}';
const basecolor = '{{basecolor}}';
const has_legends = '{{has_legends}}' === 'True';
const is_static = '{{is_static}}' === 'True';
const default_legend = '{{default_legend}}' === 'True';

init({
Expand All @@ -20,6 +21,7 @@ document
basemap,
basecolor,
has_legends,
is_static,
default_legend
});
});
9 changes: 9 additions & 0 deletions cartoframes/assets/init_grid.js.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const maps = {{ maps|tojson }};
const show_info = '{{show_info}}' === 'True';
const is_static = '{{is_static}}' === 'True';

init({
show_info,
is_static,
maps
});
154 changes: 106 additions & 48 deletions cartoframes/assets/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ var init = (function () {
return value.toLocaleString();
}

function updateViewport(map) {
function updateMapInfo() {
const mapInfo$ = document.getElementById('map-info');

const center = map.getCenter();
const lat = center.lat.toFixed(6);
const lng = center.lng.toFixed(6);
const zoom = map.getZoom().toFixed(2);

mapInfo$.innerText = `viewport={'zoom': ${zoom}, 'lat': ${lat}, 'lng': ${lng}}`;
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
}

updateMapInfo();

map.on('zoom', updateMapInfo);
map.on('move', updateMapInfo);
}

function getBasecolorSettings(basecolor) {
return {
'version': 8,
'sources': {},
'layers': [{
'id': 'background',
'type': 'background',
'paint': {
'background-color': basecolor
}
}]
};
}

function createDefaultLegend(layers) {
const defaultLegendContainer = document.querySelector('#defaultLegendContainer');
defaultLegendContainer.style.display = 'none';
Expand All @@ -52,8 +84,8 @@ var init = (function () {
);
}

function createLegend(layer, legendData, layerIndex) {
const element = document.querySelector(`#layer${layerIndex}_legend`);
function createLegend(layer, legendData, layerIndex, mapIndex=0) {
const element = document.querySelector(`#layer${layerIndex}_map${mapIndex}_legend`);
elenatorro marked this conversation as resolved.
Show resolved Hide resolved

if (legendData.prop) {
const config = { othersLabel: 'Others' }; // TODO: i18n
Expand Down Expand Up @@ -368,59 +400,67 @@ var init = (function () {
return { clickAttrs, hoverAttrs };
}

function setReady (settings) {
const BASEMAPS = {
DarkMatter: carto.basemaps.darkmatter,
Voyager: carto.basemaps.voyager,
Positron: carto.basemaps.positron
};

const attributionControl = new mapboxgl.AttributionControl({
compact: false
});

const FIT_BOUNDS_SETTINGS = { animate: false, padding: 50, maxZoom: 14 };

async function setReady(settings) {
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
try {
onReady(settings);
const maps = settings.maps ?
await initMaps(settings.maps)
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
: await initMap(settings);
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
displayError(e);
}
}

function onReady(settings) {
const BASEMAPS = {
DarkMatter: carto.basemaps.darkmatter,
Voyager: carto.basemaps.voyager,
Positron: carto.basemaps.positron
};

const BASECOLOR = {
'version': 8,
'sources': {},
'layers': [{
'id': 'background',
'type': 'background',
'paint': {
'background-color': settings.basecolor
}
}]
};

if (settings.mapboxtoken) {
mapboxgl.accessToken = settings.mapboxtoken;
}

const basemapStyle = BASEMAPS[settings.basemap] || settings.basemap || BASECOLOR;
async function saveImage(mapIndex) {
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
const img = mapIndex !== undefined ? `map-image-${mapIndex}` : 'map-image';
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
const container = mapIndex !== undefined ? `main-container-${mapIndex}` : 'main-container';
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
const $img = document.getElementById(img);
const $container = document.getElementById(container);

html2canvas($container)
.then((canvas) => {
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
const src = canvas.toDataURL();
$img.setAttribute('src', src);
$img.style.display = 'block';
$container.style.display = 'none';
});
}

const map = new mapboxgl.Map({
container: 'map',
style: basemapStyle,
zoom: 9,
dragRotate: false
async function initMaps(maps) {
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
return await maps.map(async function (mapSettings, mapIndex) {
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
return await initMap(mapSettings, mapIndex);
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
});
}

map.fitBounds(settings.bounds, { animate: false, padding: 50, maxZoom: 14 });
async function initMap(settings, mapIndex) {
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
const basecolor = getBasecolorSettings(settings.basecolor);
const basemapStyle = BASEMAPS[settings.basemap] || settings.basemap || basecolor;
const container = mapIndex !== undefined ? `map-${mapIndex}` : 'map';
elenatorro marked this conversation as resolved.
Show resolved Hide resolved
const map = createMap(container, basemapStyle, settings.bounds, settings.mapboxtoken);

if (settings.show_info) {
const updateMapInfo = _updateMapInfo.bind(this, map);

map.on('zoom', updateMapInfo);
map.on('move', updateMapInfo);
updateViewport(map);
}

if (settings.camera) {
map.flyTo(settings.camera);
}

return await initLayers(map, settings, mapIndex);
}

async function initLayers(map, settings, mapIndex) {
const mapLayers = [];
const interactiveLayers = [];
const interactiveMapLayers = [];
Expand All @@ -435,8 +475,8 @@ var init = (function () {

try {
mapLayer._updateLayer.catch(displayError);
} catch (err) {
throw err;
} catch (e) {
throw e;
}

mapLayer.addTo(map);
Expand All @@ -447,7 +487,7 @@ var init = (function () {
}

if (settings.has_legends && layer.legend) {
createLegend(mapLayer, layer.legend, settings.layers.length - index - 1);
createLegend(mapLayer, layer.legend, settings.layers.length - index - 1, mapIndex);
}

if (layer.widgets.length) {
Expand Down Expand Up @@ -479,17 +519,35 @@ var init = (function () {
if (settings.default_legend) {
createDefaultLegend(mapLayers);
}

return new Promise((resolve) => {
carto.on('loaded', mapLayers, () => {
if (settings.is_static) {
saveImage(mapIndex);
}

resolve(mapLayers);
});
});
}

function _updateMapInfo(map) {
const mapInfo$ = document.getElementById('map-info');
function createMap(container, basemapStyle, bounds, accessToken) {
if (accessToken) {
mapboxgl.accessToken = accessToken;
}

const map = new mapboxgl.Map({
container,
style: basemapStyle,
zoom: 9,
dragRotate: false,
attributionControl: false
});

const center = map.getCenter();
const lat = center.lat.toFixed(6);
const lng = center.lng.toFixed(6);
const zoom = map.getZoom().toFixed(2);
map.addControl(attributionControl);
map.fitBounds(bounds, FIT_BOUNDS_SETTINGS);

mapInfo$.innerText = `viewport={'zoom': ${zoom}, 'lat': ${lat}, 'lng': ${lng}}`;
return map;
}

function init(settings) {
Expand Down
4 changes: 2 additions & 2 deletions cartoframes/assets/src/legends.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function createDefaultLegend(layers) {
);
}

export function createLegend(layer, legendData, layerIndex) {
const element = document.querySelector(`#layer${layerIndex}_legend`);
export function createLegend(layer, legendData, layerIndex, mapIndex=0) {
const element = document.querySelector(`#layer${layerIndex}_map${mapIndex}_legend`);
elenatorro marked this conversation as resolved.
Show resolved Hide resolved

if (legendData.prop) {
const config = { othersLabel: 'Others' }; // TODO: i18n
Expand Down
Loading