Skip to content
Snippets Groups Projects
Unverified Commit 4abc29e7 authored by Fabian's avatar Fabian Committed by GitHub
Browse files

Feat/map initial design (#2)


* show multiple poi & center seleceted poi

* default export Map & bugfixes

Co-authored-by: default avatarFabian Schmidt <fabian@ds-fs.de>
parent 833d894a
No related branches found
Tags v0.0.4
No related merge requests found
This diff is collapsed.
import React, { useState } from 'react';
import type { LatLngExpression } from 'leaflet';
import { TileLayer, MapContainer, Marker, Popup } from 'react-leaflet';
import './App.css';
import SidebarListView from './Sidebar/SidebarListView';
import data from './testData.json';
import type { PointOfInterest } from './types/PointOfInterest';
import SidebarSingleView from './Sidebar/SidebarSingleView';
import Map from './Map/Map';
interface AppProps {}
function App({}: AppProps) {
const hamburgCenter: LatLngExpression = [53.550359, 9.986701];
const [selectedPoi, setSelectedPoi] = useState<null | PointOfInterest>(null);
const handlePoiClick = (id: number) => {
......@@ -32,29 +30,7 @@ function App({}: AppProps) {
<SidebarListView style={{ flex: 1 }} values={data as PointOfInterest[]} onClick={handlePoiClick} />
)}
<MapContainer
id={'mapid'}
className={'h-full w-full'}
style={{ flex: 3 }}
center={hamburgCenter}
zoom={13}
scrollWheelZoom={true}
>
<TileLayer
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}"
id="mapbox/streets-v11"
tileSize={512}
accessToken="pk.eyJ1IjoicHJleWEyayIsImEiOiJja202N2JucGowbGU4MnB1aWtxNGkzMW9jIn0.rVBLRZtohLEgdSLO0nlWng"
zoomOffset={-1}
maxZoom={18}
/>
<Marker position={hamburgCenter}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
<Map values={data as PointOfInterest[]} onSelect={handlePoiClick} selectedEntry={selectedPoi} />
</div>
);
}
......
import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet';
import React from 'react';
import type { PointOfInterest } from '../types/PointOfInterest';
import type { LatLngExpression } from 'leaflet';
import MapViewController from './MapViewController';
interface Props {
values: PointOfInterest[];
onSelect: (id: number) => void;
selectedEntry?: PointOfInterest | null;
}
const DEFAULT_CENTER: LatLngExpression = [53.550359, 9.986701];
export const Map: React.FC<Props> = (props) => {
return (
<MapContainer
id={'mapid'}
className={'h-full w-full'}
style={{ flex: 3 }}
center={DEFAULT_CENTER}
zoom={13}
scrollWheelZoom={true}
>
<TileLayer
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}"
id="mapbox/streets-v11"
tileSize={512}
accessToken="pk.eyJ1IjoicHJleWEyayIsImEiOiJja202N2JucGowbGU4MnB1aWtxNGkzMW9jIn0.rVBLRZtohLEgdSLO0nlWng"
zoomOffset={-1}
maxZoom={18}
/>
<MapViewController center={props.selectedEntry?.latlng ?? DEFAULT_CENTER} zoom={13} />
{!!props.selectedEntry && <Marker position={props.selectedEntry.latlng} />}
{!props.selectedEntry &&
props.values.map((poi) => (
<Marker key={poi.id} position={poi.latlng} eventHandlers={{ click: () => props.onSelect(poi.id) }} />
))}
</MapContainer>
);
};
export default Map;
import type { LatLngExpression } from 'leaflet';
import React, { useEffect } from 'react';
import { useMap } from 'react-leaflet';
interface Props {
center: LatLngExpression;
zoom: number;
}
const MapViewController: React.FC<Props> = ({ center, zoom }) => {
const map = useMap();
useEffect(() => {
map.setView(center, zoom);
}, [center, zoom]);
return <></>;
};
export default MapViewController;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment