{% extends "base.html" %} {% block legend %} {% if showLegend %} {% if colorStops and colorProperty and widthProperty %} {% if colorProperty != widthProperty %} calcColorLegend({{ colorStops }}, "{{ colorProperty }} vs. {{ widthProperty }}"); {% else %} calcColorLegend({{ colorStops }}, "{{ colorProperty }}"); {% endif %} {% elif colorStops and colorProperty %} calcColorLegend({{ colorStops }}, "{{ colorProperty }}"); {% endif %} {% endif %} {% endblock legend %} {% block map %} map.on('style.load', function() { {% block linestring %} // Add geojson data source map.addSource("data", { "type": "geojson", "data": {{ geojson_data }}, "buffer": 1, "maxzoom": 14 }); // Add data layer map.addLayer({ "id": "linestring", "source": "data", "type": "line", "layout": { "line-join": "round", "line-cap": "round" }, "paint": { {% if lineDashArray %} "line-dasharray": {{ lineDashArray }}, {% endif %} {% if colorProperty %} "line-color": generatePropertyExpression("{{ colorType }}", "{{ colorProperty }}", {{ colorStops }}, "{{ defaultColor }}"), {% else %} "line-color": "{{ defaultColor }}", {% endif %} {% if widthProperty %} "line-width": generatePropertyExpression("{{ widthType }}", "{{ widthProperty }}", {{ widthStops }}, {{ defaultWidth }}), {% else %} "line-width": {{ defaultWidth }}, {% endif %} "line-opacity": {{ opacity }} } }, "{{ belowLayer }}" ); // Add label layer map.addLayer({ "id": "linestring-label", "source": "data", "type": "symbol", "layout": { {% if labelProperty %} "text-field": "{{ labelProperty }}", {% endif %} "text-size" : generateInterpolateExpression('zoom', [[0, {{ labelSize }}],[22, 3* {{ labelSize }}]] ), "text-offset": [0,-1] }, "paint": { "text-halo-color": "{{ labelHaloColor }}", "text-halo-width": generatePropertyExpression('interpolate', 'zoom', [[0,{{ labelHaloWidth }}], [18,5* {{ labelHaloWidth }}]]), "text-color": "{{ labelColor }}" } }, "{{belowLayer}}" ); {% endblock linestring %} // Popups {% if popupOpensOnHover %} var popupAction = 'mousemove', popupSettings = { closeButton: false, closeOnClick: false }; {% else %} var popupAction = 'click', popupSettings = { closeButton: true, closeOnClick: true }; {% endif %} // Create a popup var popup = new mapboxgl.Popup(popupSettings); {% block linestring_popup %} // Show the popup on mouseover map.on(popupAction, 'linestring', function(e) { {% if popupOpensOnHover %} map.getCanvas().style.cursor = 'pointer'; {% endif %} let f = e.features[0]; let popup_html = '
'; for (key in f.properties) { popup_html += '
  • ' + key + ': ' + f.properties[key] + '
  • ' } popup_html += '
    ' popup.setLngLat(e.lngLat) .setHTML(popup_html) .addTo(map); }); {% endblock linestring_popup %} // change cursor to pointer when mouse is over the linestring feature layer map.on('mouseenter', 'linestring', function () { map.getCanvas().style.cursor = 'pointer'; }); // reset cursor to pointer when mouse leaves the linestring feature layer map.on('mouseleave', 'linestring', function() { map.getCanvas().style.cursor = ''; {% if popupOpensOnHover %} popup.remove(); {% endif %} }); // Fly to on click map.on('click', 'linestring', function(e) { map.flyTo({ center: e.lngLat, zoom: map.getZoom() + 1 }); }); }); {% endblock map %}