dyomedea/src/components/map/Map.tsx

310 lines
8.6 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 { useGeographic as olUseGeographic } from 'ol/proj';
import DragRotate from 'ol/interaction/DragRotate';
2022-11-25 15:45:26 +00:00
import 'ol/ol.css';
import './Map.css';
2023-01-13 13:02:58 +00:00
import { Collection, VectorTile } 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-28 18:33:02 +00:00
import MapTileProvider, { mapTileProviders } from '../map-tile-provider';
import Interaction from 'ol/interaction/Interaction';
import DoubleClickZoom from 'ol/interaction/DoubleClickZoom';
import DragPan from 'ol/interaction/DragPan';
import PinchRotate from 'ol/interaction/PinchRotate';
import PinchZoom from 'ol/interaction/PinchZoom';
import KeyboardPan from 'ol/interaction/KeyboardPan';
import KeyboardZoom from 'ol/interaction/KeyboardZoom';
import MouseWheelZoom from 'ol/interaction/MouseWheelZoom';
import DragZoom from 'ol/interaction/DragZoom';
2022-11-29 21:12:50 +00:00
import Infos, { clickHandler } from '../infos';
import GpxDialog from '../gpx-dialog';
2022-12-13 15:16:30 +00:00
import GpxRecord from '../gpx-record';
import dispatch from '../../workers/dispatcher-main';
2022-12-14 07:22:51 +00:00
import { debounce } from 'lodash';
2022-11-25 20:09:59 +00:00
2022-12-15 18:45:30 +00:00
import { AndroidFullScreen } from '@awesome-cordova-plugins/android-full-screen';
2022-12-25 15:30:47 +00:00
import Account from '../account';
2023-01-09 19:51:17 +00:00
import OsmFetch from '../osm-fetch';
2023-01-13 13:02:58 +00:00
import VectorTileLayer from 'ol/layer/VectorTile';
import VectorTileSource from 'ol/source/VectorTile.js';
import MVT from 'ol/format/MVT.js';
2023-01-15 14:42:53 +00:00
import style from '../gpx/styles';
2022-12-15 18:45:30 +00:00
2022-11-25 20:09:59 +00:00
const [getState, setState] = createSignal({
lon: 0,
lat: 0,
rotation: 0,
zoom: 0,
provider: 'osm',
2022-11-25 20:09:59 +00:00
});
export { getState };
2022-11-25 15:45:26 +00:00
2022-12-10 15:23:10 +00:00
const [getMap, setMap] = createSignal<OlMap | null>(null);
export { getMap };
2022-11-25 15:45:26 +00:00
const Map: Component = () => {
2022-11-25 20:09:59 +00:00
const navigate = useNavigate();
const params = useParams();
2022-12-15 18:45:30 +00:00
// See https://stackoverflow.com/questions/71288670/how-to-make-fullscreen-ionic-capacitor-app
AndroidFullScreen.isImmersiveModeSupported()
.then(() => AndroidFullScreen.immersiveMode())
.catch(console.warn);
if (window.plugins) {
window.plugins.intentShim.registerBroadcastReceiver(
{
filterActions: ['android.intent.action.VIEW'],
},
function (intent: any) {
console.log({
caller: 'Intent broadcast receiver',
intent,
});
}
);
window.plugins.intentShim.onIntent(function (intent: any) {
console.log({ caller: 'Intent receiver', intent });
const url = new URL(intent.data);
const q = url.search;
const [, lat, lon] = q.match(/q=([0-9.]+),([0-9.]+)/);
const zoom = Math.min(18, getState().zoom);
navigate(
`/map/${getState().provider}/${lon}/${lat}/${zoom}/${
getState().rotation
}`
);
});
} else {
console.log({
caller: 'Intent',
message: "window.plugins doesn't exist",
window,
});
}
if (
params.lat === '0' &&
params.lon === '0' &&
params.provider === 'osm' &&
params.rotation === '0' &&
params.zoom === '2'
) {
dispatch({ action: 'getState' }, (error, state) => {
if (state !== null) {
console;
navigate(
`/map/${state.provider}/${state.lon}/${state.lat}/${state.zoom}/${state.rotation}`
);
}
});
}
2022-11-25 20:09:59 +00:00
let target: HTMLDivElement;
2022-12-14 07:22:51 +00:00
const debouncedDbSetState = debounce((state: any) => {
console.log({ caller: 'Map / debouncedDbSetState', state });
dispatch({ action: 'setState', params: state });
}, 60000);
createEffect(async () => {
console.log({
2022-11-28 18:33:02 +00:00
caller: 'Map / setState',
params: {
...params,
},
});
2022-11-25 20:09:59 +00:00
setState({
provider: params.provider,
2022-11-25 20:09:59 +00:00
lon: +params.lon,
lat: +params.lat,
rotation: +params.rotation,
zoom: +params.zoom,
});
2022-12-14 07:22:51 +00:00
debouncedDbSetState(getState());
2022-11-25 15:45:26 +00:00
const map = getMap();
2022-11-28 18:33:02 +00:00
const layers = map?.getLayers();
const tileLayer = layers?.item(0) as TileLayer<any> | undefined;
if (tileLayer?.get('provider') !== params.provider) {
tileLayer?.set('provider', params.provider, true);
tileLayer?.setSource(mapTileProviders[params.provider].source);
}
2022-11-25 15:45:26 +00:00
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: 'obstacle',
2022-11-25 21:12:44 +00:00
// @ts-ignore
src: ShowLocationIcon,
}),
});
const feature = new Feature({
geometry: point,
// labelPoint: point,
// name: 'current location',
style: style,
});
2022-11-29 21:12:50 +00:00
feature.set('type', 'current-location');
2022-11-25 21:12:44 +00:00
feature.setStyle(style);
source.addFeature(feature);
// source.changed();
console.log({
caller: 'Map / updateLocation',
location,
source,
style,
feature,
});
}
});
onMount(async () => {
2022-11-25 15:45:26 +00:00
olUseGeographic();
const changeListener = (event: any) => {
const map = getMap();
const view = map?.getView();
const center = view?.getCenter();
if (center) {
navigate(
`/map/${getState().provider}/${center[0]}/${
2022-11-25 15:45:26 +00:00
center[1]
}/${view?.getZoom()}/${view?.getRotation()}`
);
}
console.log({
caller: 'Map / changeListener',
event,
2022-11-28 18:33:02 +00:00
params: {
...params,
},
2022-11-25 15:45:26 +00:00
map,
});
};
2022-11-28 18:33:02 +00:00
const tileLayer = new TileLayer({
source: mapTileProviders[params.provider].source,
});
tileLayer.set('provider', params.provider, true);
const vectorLayer = new VectorLayer({
source: new VectorSource(),
zIndex: Infinity,
});
2023-01-15 14:42:53 +00:00
const vectorTileSource1 = new VectorTileSource({
2023-01-13 13:02:58 +00:00
format: new MVT(),
2023-01-15 14:42:53 +00:00
url: 'http://localhost:8081/services/aragon/tiles/{z}/{x}/{y}.pbf',
maxZoom: 14,
2023-01-13 13:02:58 +00:00
});
2023-01-15 14:42:53 +00:00
const vectorTileLayer1 = new VectorTileLayer({
source: vectorTileSource1,
style: style,
2023-01-15 18:29:09 +00:00
declutter: false,
2023-01-15 14:42:53 +00:00
});
const vectorTileSource2 = new VectorTileSource({
format: new MVT(),
url: 'http://localhost:8080/geoserver/gwc/service/tms/1.0.0/dyomedea%3Ahiking_routes@EPSG%3A900913@pbf/{z}/{x}/{-y}.pbf',
});
const vectorTileLayer2 = new VectorTileLayer({
source: vectorTileSource2,
2023-01-13 13:02:58 +00:00
});
2022-11-25 15:45:26 +00:00
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
}),
2023-01-15 14:42:53 +00:00
layers: [tileLayer, vectorTileLayer1, vectorLayer],
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 }),
]),
moveTolerance: 10,
interactions: new Collection<Interaction>([
new DragRotate(),
new DoubleClickZoom(),
new DragPan(),
new PinchRotate(),
new PinchZoom(),
new KeyboardPan(),
new KeyboardZoom(),
new MouseWheelZoom(),
new DragZoom(),
]),
2022-11-25 15:45:26 +00:00
});
olMap.on(['moveend'], changeListener);
olMap.on(['singleclick'], clickHandler);
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}>
2023-01-09 19:51:17 +00:00
<OsmFetch map={getMap} />
<GetLocation />
<Forward />
<Back />
2022-12-13 15:16:30 +00:00
<GpxRecord />
<GpxImport />
2022-11-28 18:33:02 +00:00
<MapTileProvider />
<GpxDialog />
2022-12-25 15:30:47 +00:00
<Account />
2022-11-27 14:50:08 +00:00
<AllGpxes map={getMap} />
2022-11-29 21:12:50 +00:00
<Infos />
</div>
);
2022-11-25 15:45:26 +00:00
};
export default Map;