Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/circle/MapKitCircleOverlayRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type CircleOverlayRenderer,
} from '@mapconductor/js-sdk-core';
import { MapKitViewHolder } from '../MapKitViewHolder';
import { toCoordinates, toMapKitStyleColor } from '../helpers';
import { toCoordinates, toMapKitStyleColor, addStackedOverlay } from '../helpers';
import type { MapKitActualCircle } from '../MapKitTypeAlias';

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ export class MapKitCircleOverlayRenderer implements CircleOverlayRenderer<MapKit
// MapKitViewController.handleCircleClick).
enabled: false,
});
this.map.addOverlay(overlay);
addStackedOverlay(this.map, overlay, 'circle', state.zIndex);
return overlay;
}

Expand Down
39 changes: 38 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createGeoPoint, type GeoPoint, type GeoPointInterface } from '@mapconductor/js-sdk-core';
import {
compareOverlayStackKeys,
createGeoPoint,
type GeoPoint,
type GeoPointInterface,
type OverlayKind,
type OverlayStackKey,
} from '@mapconductor/js-sdk-core';

/** A CSS color split into an opaque color string and a separate opacity, the
* shape MapKit JS's `Style` (fillColor + fillOpacity / strokeColor +
Expand Down Expand Up @@ -92,3 +99,33 @@ export function toUnwrappedCoordinates(points: readonly GeoPointInterface[]): ma
export function toGeoPoint(coordinate: mapkit.Coordinate): GeoPoint {
return createGeoPoint({ latitude: coordinate.latitude, longitude: coordinate.longitude });
}

// Stacking keys of the vector overlays this SDK added.
const overlayStackKeys = new WeakMap<object, OverlayStackKey>();
let overlayStackSequence = 0;

/**
* Adds a polygon / circle / polyline overlay at its place in the shared stack
* (js-sdk-core OVERLAY_STACK_ORDER: polygon < circle < polyline, zIndex within the band, equal
* zIndexes in the order they were handed over).
*
* MapKit JS has no z-order for overlays: it draws them in the order of `map.overlays`. Overlays
* that belong above the new one are taken off and put back after it. Adding them in whatever
* order they arrived used to decide which kind was on top.
*/
export function addStackedOverlay(
map: mapkit.Map,
overlay: mapkit.Overlay,
kind: OverlayKind,
zIndex: number | null | undefined = 0,
): void {
const key: OverlayStackKey = { kind, zIndex: zIndex ?? 0, sequence: ++overlayStackSequence };
overlayStackKeys.set(overlay, key);
const above = map.overlays.filter((other) => {
const otherKey = overlayStackKeys.get(other);
return otherKey != null && compareOverlayStackKeys(otherKey, key) > 0;
});
if (above.length > 0) map.removeOverlays(above);
map.addOverlay(overlay);
if (above.length > 0) map.addOverlays(above);
}
4 changes: 2 additions & 2 deletions src/polygon/MapKitPolygonOverlayRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type PolygonOverlayRenderer,
} from '@mapconductor/js-sdk-core';
import { MapKitViewHolder } from '../MapKitViewHolder';
import { toCoordinates, toMapKitStyleColor } from '../helpers';
import { toCoordinates, toMapKitStyleColor, addStackedOverlay } from '../helpers';
import type { MapKitActualPolygon } from '../MapKitTypeAlias';

// Match the ArcGIS/native geodesic densification cap so world-mask rings stay a
Expand Down Expand Up @@ -41,7 +41,7 @@ export class MapKitPolygonOverlayRenderer implements PolygonOverlayRenderer<MapK
// MapKitViewController.handlePolygonClick).
enabled: false,
});
this.map.addOverlay(overlay);
addStackedOverlay(this.map, overlay, 'polygon', state.zIndex);
return overlay;
}

Expand Down
4 changes: 2 additions & 2 deletions src/polyline/MapKitPolylineOverlayRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type PolylineOverlayRenderer,
} from '@mapconductor/js-sdk-core';
import { MapKitViewHolder } from '../MapKitViewHolder';
import { toCoordinates, toMapKitStyleColor } from '../helpers';
import { toCoordinates, toMapKitStyleColor, addStackedOverlay } from '../helpers';
import type { MapKitActualPolyline } from '../MapKitTypeAlias';

/**
Expand All @@ -32,7 +32,7 @@ export class MapKitPolylineOverlayRenderer implements PolylineOverlayRenderer<Ma
// map's single-tap coordinate (see MapKitViewController.handlePolylineClick).
enabled: false,
});
this.map.addOverlay(overlay);
addStackedOverlay(this.map, overlay, 'polyline', state.zIndex);
return overlay;
}

Expand Down