fix(map): group collocated user markers
This commit is contained in:
@@ -33,6 +33,32 @@ export function parseUserLocation(value: unknown): ParsedUserLocation | null {
|
||||
return { latitude, longitude, label: label || "Approximate location unavailable" };
|
||||
}
|
||||
|
||||
export function groupMapLocations<T extends {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
nickname: string;
|
||||
}>(locations: T[]) {
|
||||
const grouped = new Map<string, { latitude: number; longitude: number; locations: T[] }>();
|
||||
for (const location of locations) {
|
||||
const roundedLatitude = Number(location.latitude.toFixed(2));
|
||||
const latitude = roundedLatitude === 0 ? 0 : roundedLatitude;
|
||||
const roundedLongitude = Number(location.longitude.toFixed(2));
|
||||
const longitude = Math.abs(roundedLongitude) === 180 ? -180 : roundedLongitude;
|
||||
const key = `${latitude}:${longitude}`;
|
||||
const group = grouped.get(key);
|
||||
if (group) group.locations.push(location);
|
||||
else grouped.set(key, { latitude, longitude, locations: [location] });
|
||||
}
|
||||
return [...grouped.entries()].map(([key, group]) => ({
|
||||
key,
|
||||
latitude: group.latitude,
|
||||
longitude: group.longitude,
|
||||
count: group.locations.length,
|
||||
nicknames: [...group.locations.map((location) => location.nickname)].sort((left, right) => left.localeCompare(right)),
|
||||
locations: group.locations,
|
||||
}));
|
||||
}
|
||||
|
||||
export function projectWorldPoint(latitude: number, longitude: number, width: number, height: number) {
|
||||
return {
|
||||
x: ((longitude + 180) / 360) * width,
|
||||
|
||||
Reference in New Issue
Block a user