This commit is contained in:
@ -19,7 +19,7 @@ function getFeatureDistance(f: GeoJSON.Feature, point: [number, number]) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getMeta(coord: WorldLocation, layer: string) {
|
||||
export async function getFeature(coord: WorldLocation, layer: string, filter?: (feature: GeoJSON.Feature) => boolean) {
|
||||
const zxy = coordToTile(coord, 14);
|
||||
const tile = await fetchTile(zxy.z, zxy.x, zxy.y);
|
||||
const layerData = tile.layers[layer];
|
||||
@ -33,13 +33,18 @@ export async function getMeta(coord: WorldLocation, layer: string) {
|
||||
const filtered = features.filter(
|
||||
(f) =>
|
||||
f.geometry.type === "LineString" || f.geometry.type == "MultiLineString",
|
||||
);
|
||||
).filter((f) => (filter ? filter(f) : true));
|
||||
if (filtered.length === 0) return null;
|
||||
const nearest = filtered.reduce((a, b) => {
|
||||
const distA = getFeatureDistance(a, [coord.lon, coord.lat]);
|
||||
const distB = getFeatureDistance(b, [coord.lon, coord.lat]);
|
||||
return distA < distB ? a : b;
|
||||
});
|
||||
return nearest;
|
||||
}
|
||||
|
||||
export async function getMeta(coord: WorldLocation, layer: string, filter?: (feature: GeoJSON.Feature) => boolean) {
|
||||
const nearest = await getFeature(coord, layer, filter);
|
||||
return nearest ? nearest.properties : null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user