maps.chandlerswift.com/layers/chains/culvers/layer.js

46 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-07-04 00:56:31 -05:00
import VectorLayer from 'ol/layer/Vector';
import {Vector as VectorSource} from 'ol/source.js';
import GeoJSON from 'ol/format/GeoJSON.js';
2024-03-12 00:10:25 -05:00
import { toLonLat } from 'ol/proj';
2023-07-04 00:56:31 -05:00
import {Style} from 'ol/style.js';
import Icon from 'ol/style/Icon.js';
import url from './data.geojson?url'; // TODO: remove `?url`?
import pin from './pin.svg?url'; // TODO: remove `?url`?
2023-07-04 00:56:31 -05:00
const vectorLayer = new VectorLayer({
source: new VectorSource({
url: url,
2023-07-04 00:56:31 -05:00
format: new GeoJSON,
}),
style: new Style({
image: new Icon({
anchor: [0.5, 1],
src: pin,
2023-07-04 00:56:31 -05:00
}),
}),
});
2024-03-12 00:10:25 -05:00
vectorLayer.customPopupCallback = async function(feature) {
const fotd_parent = document.createElement('div');
fotd_parent.innerHTML = "Flavor of the Day: ";
document.querySelector('#popup-content').append(fotd_parent);
const fotd_child = document.createElement('span');
fotd_child.innerHTML = "Loading…";
const fotd_image = document.createElement('img')
fotd_parent.append(fotd_child, fotd_image);
const [long, lat] = toLonLat(feature.getGeometry().getCoordinates());
2024-10-19 16:19:02 -05:00
const res = await fetch(`https://corsproxy.io/?${encodeURIComponent(`https://www.culvers.com/api/locator/getLocations?lat=${lat}&long=${long}&limit=1&t=${Date.now()}`)}`)
2024-03-12 00:10:25 -05:00
const res_data = await res.json();
fotd_child.innerHTML = res_data.data.geofences[0].metadata.flavorOfDayName;
fotd_image.src = `https://cdn.culvers.com/menu-item-detail/${res_data.data.geofences[0].metadata.flavorOfDaySlug}`;
fotd_image.style.width = "min(300px, 60vh)";
}
2023-07-04 00:56:31 -05:00
export default vectorLayer;