function getColor(cluster: any): string {
if (cluster.markers.length > 1) {
return "#007AFF";
}
return cluster.markers[0].available ? "#34C759" : "#FF3B30";
}
export default MapAppleCluster;
Assistant
🎯 Problem
The thresholds you hard-coded in getThreshold() are nothing more than “educated guesses”.
With ±4 000 points on a small country such as Poland they very quickly become either
too large → everything ends up in one big blob, or
too small → you render hundreds of single-pin annotations.
⚙️ A reproducible, resolution-independent way to “calibrate” the clustering radius is to express it in screen pixels, not degrees.
A common choice is 30–60 px (Google Maps uses 44 px by default).
Because MapKit JS gives you
• the span of the current region (in degrees)
• the actual size of the map DIV (in pixels)
you can convert “N pixels” ⟶ “N degrees” on every region-change.
That makes the clustering automatically adapt to zoom level, latitude and even device DPI.
Step-by-step
1️⃣ Replace the static lookup table with a function that produces a
threshold in degrees for the current map view.
ts
/**
* How many screen pixels should belong to one cluster.
* Play with this constant – 40-50 px feels right on a normal phone.
*/
const CLUSTER_RADIUS_PX = 48;
function getDynamicThreshold(map: any): { lat: number; lng: number } {
const div = map._element as HTMLDivElement; // MapKit JS keeps the <div>
const { width, height } = div.getBoundingClientRect();
// Region span (degrees) that is currently visible
const span = map.region.span;
// How many degrees correspond to 1 pixel
const degPerPxLat = span.latitudeDelta / height;
const degPerPxLng = span.longitudeDelta / width;
return {
lat: degPerPxLat * CLUSTER_RADIUS_PX,
lng: degPerPxLng * CLUSTER_RADIUS_PX,
};
}
2️⃣ Re-write the clustering routine so it accepts a pair of
thresholds instead of one number:
3️⃣ Inside your updateClusters callback calculate the dynamic thresholds
and feed them into the new function:
ts
const updateClusters = () => {
map.removeAnnotations(map.annotations);
const { lat: tLat, lng: tLng } = getDynamicThreshold(map);
// Optional: slice if you still want a hard limit for perf tests
const limited = markers.slice(0, markerLimit);
const clusters = clusterMarkers(limited, tLat, tLng);
const annotations = clusters.map((c) => createAnnotation(c));
map.addAnnotations(annotations);
};
4️⃣ Remove getThreshold() (or keep it only for a desktop fallback) – the
clustering distance is now automatically “calibrated” at any zoom.
Why this works
• At zoomed-out levels the map shows e.g. 20 ° × 10 °.
48 px might translate to ~0.3° → many pins merge into one cluster.
• At street level the span is maybe 0.002° × 0.001°.
48 px now equals only ~0.00001° → clusters break apart into individual
chargers.
The same constant therefore feels natural to the user no matter how far
they zoom in or out.
Tweaking tips
Try 36 px, 44 px, 64 px and log clusters.length per zoom to find a sweet
spot performance-wise.
If you want different behaviour on mobile vs desktop, multiply the
radius by window.devicePixelRatio.
For even smoother visuals you can cache clusters per zoom level or
use a proven library such as supercluster and feed its output into
MapKit JS annotations.