Comment CityMap in map.tsx
This commit is contained in:
parent
a99d257c4e
commit
2ab35b1340
@ -1,65 +1,83 @@
|
|||||||
|
import React, {
|
||||||
import React, { FC, useCallback, useEffect, useMemo, useState, useRef } from 'react';
|
FC,
|
||||||
import { AttributionControl, MapContainer, ZoomControl, useMapEvent, Pane, useMap } from 'react-leaflet';
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useState,
|
||||||
|
useRef,
|
||||||
|
} from "react";
|
||||||
|
import {
|
||||||
|
AttributionControl,
|
||||||
|
MapContainer,
|
||||||
|
ZoomControl,
|
||||||
|
useMapEvent,
|
||||||
|
Pane,
|
||||||
|
useMap,
|
||||||
|
} from "react-leaflet";
|
||||||
|
|
||||||
// import 'leaflet/dist/leaflet.css';
|
// import 'leaflet/dist/leaflet.css';
|
||||||
import './map.css';
|
import "./map.css";
|
||||||
|
|
||||||
import { apiGet } from '../apiHelpers';
|
import { apiGet } from "../apiHelpers";
|
||||||
import { initialMapViewport, mapBackgroundColor, MapTheme, LayerEnablementState } from '../config/map-config';
|
import {
|
||||||
|
initialMapViewport,
|
||||||
|
mapBackgroundColor,
|
||||||
|
MapTheme,
|
||||||
|
LayerEnablementState,
|
||||||
|
} from "../config/map-config";
|
||||||
|
|
||||||
import mapboxgl from 'mapbox-gl';
|
import mapboxgl from "mapbox-gl";
|
||||||
|
|
||||||
import { Building } from '../models/building';
|
import { Building } from "../models/building";
|
||||||
|
|
||||||
import { CityMap } from './layers/city-base-map-layer';
|
/*import { CityMap } from './layers/city-base-map-layer';*/
|
||||||
import { CityBoundaryLayer } from './layers/city-boundary-layer';
|
import { CityBoundaryLayer } from "./layers/city-boundary-layer";
|
||||||
import { BoroughBoundaryLayer } from './layers/borough-boundary-layer';
|
import { BoroughBoundaryLayer } from "./layers/borough-boundary-layer";
|
||||||
import { BoroughLabelLayer } from './layers/borough-label-layer';
|
import { BoroughLabelLayer } from "./layers/borough-label-layer";
|
||||||
import { ParcelBoundaryLayer } from './layers/parcel-boundary-layer';
|
import { ParcelBoundaryLayer } from "./layers/parcel-boundary-layer";
|
||||||
import { HistoricDataLayer } from './layers/historic-data-layer';
|
import { HistoricDataLayer } from "./layers/historic-data-layer";
|
||||||
import { HistoricMapLayer } from './layers/historic-map-layer';
|
import { HistoricMapLayer } from "./layers/historic-map-layer";
|
||||||
import { FloodBoundaryLayer } from './layers/flood-boundary-layer';
|
import { FloodBoundaryLayer } from "./layers/flood-boundary-layer";
|
||||||
import { ConservationAreaBoundaryLayer } from './layers/conservation-boundary-layer';
|
import { ConservationAreaBoundaryLayer } from "./layers/conservation-boundary-layer";
|
||||||
import { VistaBoundaryLayer } from './layers/vista-boundary-layer';
|
import { VistaBoundaryLayer } from "./layers/vista-boundary-layer";
|
||||||
import { HousingBoundaryLayer } from './layers/housing-boundary-layer';
|
import { HousingBoundaryLayer } from "./layers/housing-boundary-layer";
|
||||||
import { CreativeBoundaryLayer } from './layers/creative-boundary-layer';
|
import { CreativeBoundaryLayer } from "./layers/creative-boundary-layer";
|
||||||
import { BuildingBaseLayer } from './layers/building-base-layer';
|
import { BuildingBaseLayer } from "./layers/building-base-layer";
|
||||||
import { BuildingDataLayer } from './layers/building-data-layer';
|
import { BuildingDataLayer } from "./layers/building-data-layer";
|
||||||
import { BuildingNumbersLayer } from './layers/building-numbers-layer';
|
import { BuildingNumbersLayer } from "./layers/building-numbers-layer";
|
||||||
import { BuildingHighlightLayer } from './layers/building-highlight-layer';
|
import { BuildingHighlightLayer } from "./layers/building-highlight-layer";
|
||||||
|
|
||||||
import { Legend } from './legend';
|
import { Legend } from "./legend";
|
||||||
import SearchBox from './search-box';
|
import SearchBox from "./search-box";
|
||||||
import ThemeSwitcher from './theme-switcher';
|
import ThemeSwitcher from "./theme-switcher";
|
||||||
import DataLayerSwitcher from './data-switcher';
|
import DataLayerSwitcher from "./data-switcher";
|
||||||
import { BoroughSwitcher } from './borough-switcher';
|
import { BoroughSwitcher } from "./borough-switcher";
|
||||||
import { ParcelSwitcher } from './parcel-switcher';
|
import { ParcelSwitcher } from "./parcel-switcher";
|
||||||
import { FloodSwitcher } from './flood-switcher';
|
import { FloodSwitcher } from "./flood-switcher";
|
||||||
import { ConservationAreaSwitcher } from './conservation-switcher';
|
import { ConservationAreaSwitcher } from "./conservation-switcher";
|
||||||
import { HistoricDataSwitcher } from './historic-data-switcher';
|
import { HistoricDataSwitcher } from "./historic-data-switcher";
|
||||||
import { HistoricMapSwitcher } from './historic-map-switcher';
|
import { HistoricMapSwitcher } from "./historic-map-switcher";
|
||||||
import { VistaSwitcher } from './vista-switcher';
|
import { VistaSwitcher } from "./vista-switcher";
|
||||||
import { CreativeSwitcher } from './creative-switcher';
|
import { CreativeSwitcher } from "./creative-switcher";
|
||||||
import { HousingSwitcher } from './housing-switcher';
|
import { HousingSwitcher } from "./housing-switcher";
|
||||||
import { BuildingMapTileset } from '../config/tileserver-config';
|
import { BuildingMapTileset } from "../config/tileserver-config";
|
||||||
import { useDisplayPreferences } from '../displayPreferences-context';
|
import { useDisplayPreferences } from "../displayPreferences-context";
|
||||||
import { CategoryMapDefinition } from '../config/category-maps-config';
|
import { CategoryMapDefinition } from "../config/category-maps-config";
|
||||||
|
|
||||||
mapboxgl.accessToken = 'pk.eyJ1IjoiYWxpLWFkbGkiLCJhIjoiY2xuM2JtYjV1MGE5djJrb2d5OGp1ZWNyNiJ9.gENyP4xX6ElLAeZFlE0aDg';
|
|
||||||
|
|
||||||
|
mapboxgl.accessToken =
|
||||||
|
"pk.eyJ1IjoiYWxpLWFkbGkiLCJhIjoiY2xuM2JtYjV1MGE5djJrb2d5OGp1ZWNyNiJ9.gENyP4xX6ElLAeZFlE0aDg";
|
||||||
|
|
||||||
interface ColouringMapProps {
|
interface ColouringMapProps {
|
||||||
selectedBuildingId: number;
|
selectedBuildingId: number;
|
||||||
mode: 'basic' | 'view' | 'edit' | 'multi-edit';
|
mode: "basic" | "view" | "edit" | "multi-edit";
|
||||||
revisionId: string;
|
revisionId: string;
|
||||||
onBuildingAction: (building: Building) => void;
|
onBuildingAction: (building: Building) => void;
|
||||||
mapColourScale: BuildingMapTileset;
|
mapColourScale: BuildingMapTileset;
|
||||||
onMapColourScale: (x: BuildingMapTileset) => void;
|
onMapColourScale: (x: BuildingMapTileset) => void;
|
||||||
categoryMapDefinitions: CategoryMapDefinition[]
|
categoryMapDefinitions: CategoryMapDefinition[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ColouringMap : FC<ColouringMapProps> = ({
|
export const ColouringMap: FC<ColouringMapProps> = ({
|
||||||
mode,
|
mode,
|
||||||
revisionId,
|
revisionId,
|
||||||
onBuildingAction,
|
onBuildingAction,
|
||||||
@ -67,30 +85,27 @@ export const ColouringMap : FC<ColouringMapProps> = ({
|
|||||||
mapColourScale,
|
mapColourScale,
|
||||||
onMapColourScale,
|
onMapColourScale,
|
||||||
categoryMapDefinitions,
|
categoryMapDefinitions,
|
||||||
children
|
children,
|
||||||
}) => {
|
}) => {
|
||||||
const { darkLightTheme, darkLightThemeSwitch, showLayerSelection } = useDisplayPreferences();
|
const { darkLightTheme, darkLightThemeSwitch, showLayerSelection } =
|
||||||
|
useDisplayPreferences();
|
||||||
const [position, setPosition] = useState(initialMapViewport.position);
|
const [position, setPosition] = useState(initialMapViewport.position);
|
||||||
const [zoom, setZoom] = useState(initialMapViewport.zoom);
|
const [zoom, setZoom] = useState(initialMapViewport.zoom);
|
||||||
|
|
||||||
|
const handleLocate = useCallback((lat: number, lng: number, zoom: number) => {
|
||||||
const handleLocate = useCallback(
|
|
||||||
(lat: number, lng: number, zoom: number) => {
|
|
||||||
setPosition([lat, lng]);
|
setPosition([lat, lng]);
|
||||||
setZoom(zoom);
|
setZoom(zoom);
|
||||||
},
|
}, []);
|
||||||
[]
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleClick = useCallback(
|
const handleClick = useCallback(
|
||||||
async (e) => {
|
async (e) => {
|
||||||
const {lat, lng} = e.latlng;
|
const { lat, lng } = e.latlng;
|
||||||
const data = await apiGet(`/api/buildings/locate?lat=${lat}&lng=${lng}`);
|
const data = await apiGet(`/api/buildings/locate?lat=${lat}&lng=${lng}`);
|
||||||
const building = data?.[0];
|
const building = data?.[0];
|
||||||
onBuildingAction(building);
|
onBuildingAction(building);
|
||||||
},
|
},
|
||||||
[onBuildingAction],
|
[onBuildingAction]
|
||||||
)
|
);
|
||||||
|
|
||||||
const mapContainer = useRef(null);
|
const mapContainer = useRef(null);
|
||||||
const map = useRef(null);
|
const map = useRef(null);
|
||||||
@ -101,32 +116,40 @@ export const ColouringMap : FC<ColouringMapProps> = ({
|
|||||||
if (map.current) return; // initialize map only once
|
if (map.current) return; // initialize map only once
|
||||||
map.current = new mapboxgl.Map({
|
map.current = new mapboxgl.Map({
|
||||||
container: mapContainer.current,
|
container: mapContainer.current,
|
||||||
style: 'mapbox://styles/mapbox/streets-v12',
|
style: "mapbox://styles/mapbox/streets-v12",
|
||||||
center: [lng, lat],
|
center: [lng, lat],
|
||||||
zoom: zoom
|
zoom: zoom,
|
||||||
});
|
});
|
||||||
|
|
||||||
map.current.on('move', () => {
|
map.current.on("move", () => {
|
||||||
setLng(map.current.getCenter().lng.toFixed(4));
|
setLng(map.current.getCenter().lng.toFixed(4));
|
||||||
setLat(map.current.getCenter().lat.toFixed(4));
|
setLat(map.current.getCenter().lat.toFixed(4));
|
||||||
setZoom(map.current.getZoom().toFixed(2));
|
setZoom(map.current.getZoom().toFixed(2));
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={mapContainer} className="map-container" style={{ position: "absolute", top: "0", bottom: "0", left: "0", right: "0" }}>
|
<div
|
||||||
|
ref={mapContainer}
|
||||||
</div>
|
className="map-container"
|
||||||
|
style={{
|
||||||
|
position: "absolute",
|
||||||
|
top: "0",
|
||||||
|
bottom: "0",
|
||||||
|
left: "0",
|
||||||
|
right: "0",
|
||||||
|
}}
|
||||||
|
></div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
function ClickHandler({ onClick }: {onClick: (e) => void}) {
|
function ClickHandler({ onClick }: { onClick: (e) => void }) {
|
||||||
useMapEvent('click', (e) => onClick(e));
|
useMapEvent("click", (e) => onClick(e));
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function MapBackgroundColor({ theme}: {theme: MapTheme}) {
|
function MapBackgroundColor({ theme }: { theme: MapTheme }) {
|
||||||
const map = useMap();
|
const map = useMap();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
map.getContainer().style.backgroundColor = mapBackgroundColor[theme];
|
map.getContainer().style.backgroundColor = mapBackgroundColor[theme];
|
||||||
@ -137,7 +160,7 @@ function MapBackgroundColor({ theme}: {theme: MapTheme}) {
|
|||||||
|
|
||||||
function MapViewport({
|
function MapViewport({
|
||||||
position,
|
position,
|
||||||
zoom
|
zoom,
|
||||||
}: {
|
}: {
|
||||||
position: [number, number];
|
position: [number, number];
|
||||||
zoom: number;
|
zoom: number;
|
||||||
@ -145,9 +168,8 @@ function MapViewport({
|
|||||||
const map = useMap();
|
const map = useMap();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
map.setView(position, zoom)
|
map.setView(position, zoom);
|
||||||
}, [position, zoom]);
|
}, [position, zoom]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user