dyomedea/src/components/map/Map.tsx

194 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-11-25 15:45:26 +00:00
import { Component, createEffect, createSignal, onMount } from 'solid-js';
2022-11-25 21:12:44 +00:00
import { useParams, useNavigate } from '@solidjs/router';
2022-11-25 15:45:26 +00:00
import OlMap from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import Feature from 'ol/Feature';
import Attribution from 'ol/control/Attribution';
import Rotate from 'ol/control/Rotate';
import ScaleLine from 'ol/control/ScaleLine';
import Control from 'ol/control/Control';
import OSM from 'ol/source/OSM';
import { useGeographic as olUseGeographic } from 'ol/proj';
import 'ol/ol.css';
import './Map.css';
2022-11-25 16:10:49 +00:00
import { Collection } from 'ol';
2022-11-25 21:12:44 +00:00
import { Point } from 'ol/geom';
2022-11-27 11:29:30 +00:00
import { Style, Icon } from 'ol/style';
2022-11-25 21:12:44 +00:00
import GetLocation, { getCurrentLocation } from '../get-location';
import ShowLocationIcon from '../get-location/ShowLocationIcon.svg';
2022-11-26 11:33:26 +00:00
import { Back, Forward } from '../back-forward';
2022-11-26 21:04:08 +00:00
import GpxImport from '../gpx-import';
2022-11-27 13:32:21 +00:00
import AllGpxes from '../all-gpxes';
2022-11-25 20:09:59 +00:00
const [getState, setState] = createSignal({
lon: 0,
lat: 0,
rotation: 0,
zoom: 0,
});
export { getState };
2022-11-25 15:45:26 +00:00
const Map: Component = () => {
const [getMap, setMap] = createSignal<OlMap | null>(null);
const params = useParams();
2022-11-25 20:09:59 +00:00
const navigate = useNavigate();
let target: HTMLDivElement;
console.log({ caller: 'Map', center: params.center });
2022-11-25 15:45:26 +00:00
createEffect(() => {
2022-11-25 20:09:59 +00:00
setState({
lon: +params.lon,
lat: +params.lat,
rotation: +params.rotation,
zoom: +params.zoom,
});
2022-11-25 15:45:26 +00:00
const map = getMap();
const view = map?.getView();
2022-11-25 16:06:07 +00:00
view?.animate({
2022-11-25 20:09:59 +00:00
center: [getState().lon, getState().lat],
rotation: getState().rotation,
zoom: getState().zoom,
2022-11-25 16:06:07 +00:00
duration: 1000,
});
2022-11-25 15:45:26 +00:00
});
2022-11-25 21:12:44 +00:00
createEffect(() => {
const location = getCurrentLocation();
if (location) {
console.log({
caller: 'Map / updateLocation',
location,
});
const source = getMap()
?.getAllLayers()
.at(1)
?.getSource() as VectorSource;
source!.clear(true);
const point = new Point([location.lon, location.lat]);
const style = new Style({
image: new Icon({
// size: [20, 20],
imgSize: [24, 24],
declutterMode: 'declutter',
// @ts-ignore
src: ShowLocationIcon,
}),
});
const feature = new Feature({
geometry: point,
// labelPoint: point,
// name: 'current location',
style: style,
});
feature.setStyle(style);
source.addFeature(feature);
// source.changed();
console.log({
caller: 'Map / updateLocation',
location,
source,
style,
feature,
});
}
});
2022-11-25 15:45:26 +00:00
onMount(() => {
olUseGeographic();
const changeListener = (event: any) => {
const map = getMap();
const view = map?.getView();
const center = view?.getCenter();
if (center) {
navigate(
`/map/${center[0]}/${
center[1]
}/${view?.getZoom()}/${view?.getRotation()}`
);
}
console.log({
caller: 'Map / changeListener',
event,
params,
map,
});
};
const olMap = new OlMap({
view: new View({
2022-11-25 20:09:59 +00:00
center: [+getState().lon, +getState().lat],
zoom: +getState().zoom,
rotation: +getState().rotation,
2022-11-25 15:45:26 +00:00
}),
layers: [
new TileLayer({
source: new OSM(),
}),
2022-11-25 21:12:44 +00:00
new VectorLayer({ source: new VectorSource() }),
2022-11-25 15:45:26 +00:00
],
target: target,
2022-11-25 20:09:59 +00:00
controls: new Collection<Control>([
new Attribution({ collapsible: true }),
new Rotate(),
new ScaleLine({ bar: true }),
// // @ts-ignore
// new Control({
// // @ts-ignore
// element: (
// <div>
// <GetLocation />
// </div>
// ),
// }),
// new Control({
// // @ts-ignore
// element: (
// <div>
// <Back />
// </div>
// ),
// }),
// new Control({
// // @ts-ignore
// element: (
// <div>
// <Forward />
// </div>
// ),
// }),
// new Control({
// // @ts-ignore
// element: (
// <div>
// <GpxImport />
// </div>
// ),
// }),
2022-11-25 20:09:59 +00:00
]),
2022-11-25 15:45:26 +00:00
});
olMap.on(['moveend'], changeListener);
2022-11-25 20:09:59 +00:00
2022-11-25 15:45:26 +00:00
setMap(olMap);
});
return (
2022-11-27 11:29:30 +00:00
// @ts-ignore
<div class='ol-map' ref={target}>
<GetLocation />
<Forward />
<Back />
<GpxImport />
2022-11-27 14:50:08 +00:00
<AllGpxes map={getMap} />
</div>
);
2022-11-25 15:45:26 +00:00
};
export default Map;