UNPKG

maplibre-gl-js-amplify

Version:

MapLibre Plugin to Support Amplify Geo Integration

126 lines (125 loc) 4.96 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.drawUnclusteredLayer = void 0; const maplibre_gl_1 = require("maplibre-gl"); const constants_1 = require("./constants"); const createMarker_1 = require("./createMarker"); const popupRender_1 = require("./popupRender"); const utils_1 = require("./utils"); const HIDE_TIP = 'amplify-tip'; function drawUnclusteredLayer(sourceName, map, _a) { var { showMarkerPopup = false } = _a, options = __rest(_a, ["showMarkerPopup"]); const unclusteredLayerId = `${sourceName}-layer-unclustered-point`; let selectedId = null; function deselectPoint() { if (selectedId !== null) { map.setLayoutProperty(unclusteredLayerId, 'icon-image', 'inactive-marker'); selectedId = null; } } const popupRender = options.popupRender ? options.popupRender : (0, popupRender_1.getPopupRenderFunction)(unclusteredLayerId, options); addUnclusteredMarkerImages(map, options); const defaultUnclusteredPoint = { id: unclusteredLayerId, type: 'symbol', source: sourceName, filter: ['!', ['has', 'point_count']], layout: { 'icon-image': 'inactive-marker', }, }; map.addLayer(Object.assign({}, defaultUnclusteredPoint)); /* * Add css to header to hide default popup tip */ if (showMarkerPopup) { const element = document.getElementById(HIDE_TIP); if (!element) { const style = document.createElement('style'); style.setAttribute('id', HIDE_TIP); document.head.append(style); style.textContent = '.mapboxgl-popup-tip { display: none; }'; } } map.on('click', function () { deselectPoint(); }); /* * Set active state on markers when clicked */ map.on('click', unclusteredLayerId, function (e) { if (typeof options.onClick === 'function') options.onClick(e); selectedId = e.features[0].id; map.setLayoutProperty(unclusteredLayerId, 'icon-image', [ 'match', ['id'], selectedId, 'active-marker', 'inactive-marker', // default ]); // If popup option is set show a popup on click if (showMarkerPopup) { const selectedFeature = e.features[0]; const coordinates = selectedFeature.geometry.coordinates; if ((0, utils_1.isCoordinates)(coordinates)) { const popup = new maplibre_gl_1.Popup() .setLngLat(coordinates) .setHTML(popupRender(selectedFeature)) .setOffset(15) .addTo(map); popup.on('close', function () { if (selectedId === selectedFeature.id) deselectPoint(); }); } } }); /* * Set cursor style to pointer when mousing over point layer */ map.on('mouseover', unclusteredLayerId, function () { map.getCanvas().style.cursor = 'pointer'; }); /* * Reset cursor style when the point layer */ map.on('mouseleave', unclusteredLayerId, () => { map.getCanvas().style.cursor = ''; }); return { unclusteredLayerId }; } exports.drawUnclusteredLayer = drawUnclusteredLayer; /* * Adds marker images to the maplibre canvas to be used for rendering unclustered points */ function addUnclusteredMarkerImages(map, { selectedColor = constants_1.ACTIVE_MARKER_COLOR, selectedBorderColor = constants_1.COLOR_WHITE, selectedBorderWidth = 4, defaultBorderColor = constants_1.COLOR_WHITE, defaultBorderWidth = 4, defaultColor: fillColor = constants_1.MARKER_COLOR, markerImageElement, activeMarkerImageElement, }) { const inactiveMarker = markerImageElement || (0, createMarker_1.createMarker)({ fillColor: fillColor, strokeColor: defaultBorderColor, lineWidth: defaultBorderWidth, }); const activeMarker = activeMarkerImageElement || markerImageElement || (0, createMarker_1.createMarker)({ fillColor: selectedColor, strokeColor: selectedBorderColor, lineWidth: selectedBorderWidth, }); map.addImage('inactive-marker', inactiveMarker, { pixelRatio: 2 }); map.addImage('active-marker', activeMarker, { pixelRatio: 2 }); }