Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

support pin map views across the international date line (#62380)

Ryan Laurieac7c00caf4ef
diff --git a/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js b/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js
index ebb16cc5550..89026b9d678 100644
--- a/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js
+++ b/e2e/test/scenarios/visualizations-charts/maps.cy.spec.js
@@ -83,6 +83,42 @@ describe("scenarios > visualizations > maps", () => {
});
});
+ it("should wrap markers around the international date line correctly (metabase#5369)", () => {
+ H.createNativeQuestion(
+ {
+ name: "friends across time",
+ native: {
+ query: `
+ SELECT 'Kleavor' as name, 68 as lat, -159 as lng
+ UNION ALL
+ SELECT 'Spectrier' as name, 68 as lat, 159 as lng
+ `,
+ "template-tags": {},
+ },
+ display: "map",
+ visualization_settings: {
+ "map.region": "world",
+ "map.type": "pin",
+ "map.latitude_column": "LAT",
+ "map.longitude_column": "LNG",
+ "map.center_latitude": 67,
+ "map.center_longitude": -175,
+ "map.zoom": 4,
+ },
+ },
+ { visitQuestion: true },
+ );
+
+ cy.get(".leaflet-marker-icon").then((markers) => {
+ // should draw 4 markers
+ expect(markers).to.have.length(4);
+ cy.get(markers[0]).should("be.visible");
+ cy.get(markers[1]).should("not.be.visible"); // outside the viewport
+ cy.get(markers[2]).should("not.be.visible"); // outside the viewport
+ cy.get(markers[3]).should("be.visible");
+ });
+ });
+
it("should not assign the full name of the state as the filter value on a drill-through (metabase#14650)", () => {
cy.intercept("/app/assets/geojson/**").as("geojson");
H.visitQuestionAdhoc({
diff --git a/frontend/src/metabase/visualizations/components/LeafletMarkerPinMap.jsx b/frontend/src/metabase/visualizations/components/LeafletMarkerPinMap.jsx
index 2480534e4b3..6acf4aa4d00 100644
--- a/frontend/src/metabase/visualizations/components/LeafletMarkerPinMap.jsx
+++ b/frontend/src/metabase/visualizations/components/LeafletMarkerPinMap.jsx
@@ -25,34 +25,77 @@ export default class LeafletMarkerPinMap extends LeafletMap {
super.componentDidUpdate(prevProps, prevState);
try {
- const { pinMarkerLayer } = this;
- const { points } = this.props;
-
- const markers = pinMarkerLayer.getLayers();
- const max = Math.max(points.length, markers.length);
- for (let i = 0; i < max; i++) {
- if (i >= points.length) {
- pinMarkerLayer.removeLayer(markers[i]);
- }
- if (i >= markers.length) {
- const marker = this._createMarker(i);
- pinMarkerLayer.addLayer(marker);
- markers.push(marker);
- }
-
- if (i < points.length) {
- const { lat, lng } = markers[i].getLatLng();
- if (lng !== points[i][0] || lat !== points[i][1]) {
- markers[i].setLatLng(points[i]);
- }
- }
- }
+ this._createMarkers(this.props.points);
} catch (err) {
console.error(err);
this.props.onRenderError(err.message || err);
}
}
+ _createMarkers = (points) => {
+ const { pinMarkerLayer } = this;
+ if (!this.map) {
+ return;
+ }
+
+ const mapBounds = this.map?.getBounds?.();
+ if (!mapBounds) {
+ return;
+ }
+ const mapWest = mapBounds.getWest();
+ const mapEast = mapBounds.getEast();
+
+ // if map crosses dateline, we need wrapping
+ const crossesLeftDateline = mapWest < -180 && mapEast > -180;
+ const crossesRightDateline = mapWest < 180 && mapEast > 180;
+ const shouldGetWrappedPoints = crossesLeftDateline || crossesRightDateline;
+
+ const wrappedPoints = shouldGetWrappedPoints
+ ? points.flatMap((point, index) => {
+ const [lat, lng] = point;
+ // we need to store the data index separately
+ // because the same point can have multiple markers
+ const points = [[lat, lng, index]];
+
+ // note: for wide screens, we may need extra copies on both sides
+ if (crossesLeftDateline) {
+ // copy on the left side
+ points.push([lat, lng - 360, index]);
+ }
+
+ if (crossesRightDateline) {
+ // copy on the right side
+ points.push([lat, lng + 360, index]);
+ }
+ return points;
+ })
+ : points;
+
+ const markers = pinMarkerLayer.getLayers();
+ const max = Math.max(wrappedPoints.length, markers.length);
+ for (let i = 0; i < max; i++) {
+ if (i >= wrappedPoints.length) {
+ pinMarkerLayer.removeLayer(markers[i]); // remove excess markers
+ }
+ if (i >= markers.length) {
+ // create new markers for new points
+ const index = shouldGetWrappedPoints ? wrappedPoints[i][2] : i;
+
+ const marker = this._createMarker(index);
+ pinMarkerLayer.addLayer(marker);
+ markers.push(marker);
+ }
+
+ if (i < wrappedPoints.length) {
+ const { lat, lng } = markers[i].getLatLng();
+ // if any marker doesn't match the point, update it
+ if (lng !== wrappedPoints[i][0] || lat !== wrappedPoints[i][1]) {
+ markers[i].setLatLng(wrappedPoints[i].slice(0, 2));
+ }
+ }
+ }
+ };
+
_createMarker = (rowIndex) => {
const marker = L.marker([0, 0], { icon: this.pinMarkerIcon });
const { onHoverChange, onVisualizationClick, settings } = this.props;