map component

This commit is contained in:
mmacierzanka committed 2021-10-02 01:13:56 +02:00
1 parent c77bcb5be9
commit df19bec60c
3 files changed
+64

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
import React, { useEffect, useRef } from "react";
import OSM from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import styles from "./index.module.scss";
export const Map = (props) => {
const mapRef = useRef(null);
useEffect(() => {
const view = new View({
center: props.center ?? [0, 0],
zoom: props.zoom ?? 3,
});
const layer = new TileLayer({
source: new XYZ({
url: "https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png",
}),
});
const el = mapRef.current;
new OSM({
target: el,
view: view,
layers: [layer],
});
view.on("change:center", () => {
if (typeof props.onCenterChange === "function") props.onCenterChange(view.getCenter());
});
return () => {
el.innerHTML = "";
};
}, [props]);
return (
<div style={{ width: props.width, height: props.height }} className={styles.container}>
<div ref={mapRef} className={styles.map}></div>
</div>
);
};
+10
View File
@@ -0,0 +1,10 @@
.container {
position: relative;
}
.map {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}