Skip to content

Commit

Permalink
refactor: abstracting minor repeated MapLibre utilities, add observab…
Browse files Browse the repository at this point in the history
…le for garden layer load
  • Loading branch information
th0rgall committed Jun 18, 2024
1 parent 522041d commit 38b5b35
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
23 changes: 23 additions & 0 deletions src/lib/api/mapbox.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type maplibregl from 'maplibre-gl';

const { VITE_MAPBOX_ACCESS_TOKEN } = import.meta.env;

const headers = {
Expand All @@ -15,6 +17,19 @@ const parseAddressPiece = (address, piece) => {
return address;
};

export const lnglatToObject = ([lng, lat]: [number, number]) => ({
longitude: lng,
latitude: lat
});

export const objectToLngLat = ({
longitude,
latitude
}: {
longitude: number;
latitude: number;
}) => [longitude, latitude];

export const geocode = async (addressString: string) => {
// See response format: https://docs.mapbox.com/api/search/geocoding/#geocoding-response-object
const response = await fetch(
Expand Down Expand Up @@ -109,3 +124,11 @@ export const geocodeCountryCode = async (country_code: string) => {
return;
}
};

export const loadImg = (map: maplibregl.Map, { url, id }: { url: string; id: string }) =>
new Promise((resolve) => {
map.loadImage(url, (error, res) => {
map.addImage(id, res);
resolve(true);
});
});
45 changes: 14 additions & 31 deletions src/lib/components/Map/GardenLayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import key from './mapbox-context.js';
import { tentIcon } from '$lib/images/markers';
import { nonMemberMaxZoom } from '$lib/constants';
import { loadImg } from '$lib/api/mapbox';
import { gardenLayerLoaded } from '$lib/stores/app';
type GardenFeatureCollection = {
type: 'FeatureCollection';
Expand Down Expand Up @@ -174,6 +176,15 @@
await Promise.all(icons.map((icon) => addTentImageToMap(icon.id, icon.colors)));
};
function addPointerOnHover(layerId: string) {
map.on('mouseenter', layerId, () => {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', layerId, () => {
map.getCanvas().style.cursor = '';
});
}
const setupMarkers = async () => {
// Catch all errors to avoid having to reload when working on this component in development
try {
Expand All @@ -184,17 +195,7 @@
{ url: '/images/markers/tent-yellow.png', id: 'tent-saved' }
];
await Promise.all(
images.map(
(img) =>
new Promise((resolve) => {
map.loadImage(img.url, (error, res) => {
map.addImage(img.id, res);
resolve(true);
});
})
)
);
await Promise.all(images.map((img) => loadImg(map, img)));
calculateData();
Expand Down Expand Up @@ -304,31 +305,13 @@
map.on('click', unclusteredPointLayerId, onGardenClick);
map.on('click', savedGardenLayerId, onGardenClick);
map.on('mouseenter', clustersLayerId, () => {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', clustersLayerId, () => {
map.getCanvas().style.cursor = '';
});
map.on('mouseenter', unclusteredPointLayerId, () => {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', unclusteredPointLayerId, () => {
map.getCanvas().style.cursor = '';
});
map.on('mouseenter', savedGardenLayerId, () => {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', savedGardenLayerId, () => {
map.getCanvas().style.cursor = '';
});
[clustersLayerId, unclusteredPointLayerId, savedGardenLayerId].forEach(addPointerOnHover);
} catch (err) {
// should not error in prod
console.log(err);
} finally {
mapReady = true;
$gardenLayerLoaded = true;
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/lib/stores/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ export const appHasLoaded = derived(
export const rootModal = writable<ComponentType | null>(null);

export const close = () => rootModal.set(null);

/**
* The garden layer loads in an async way.
* This observable is useful to know when it is possible to add layers on top of it.
*/
export const gardenLayerLoaded = writable(false);

0 comments on commit 38b5b35

Please sign in to comment.