From efb5dafcdc41a2c1d337b9041a6b5fb88f4691e8 Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Sat, 12 Jun 2021 01:35:20 +0100 Subject: [PATCH] Update map position setting for react-leaflet v3 --- app/src/frontend/config/map-config.ts | 12 ++++++--- app/src/frontend/map/map.tsx | 35 ++++++++++++++++++--------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/src/frontend/config/map-config.ts b/app/src/frontend/config/map-config.ts index 3f1b5683..31a3e662 100644 --- a/app/src/frontend/config/map-config.ts +++ b/app/src/frontend/config/map-config.ts @@ -1,7 +1,11 @@ -export const defaultMapPosition = { - lat: 51.5245255, - lng: -0.1338422, - zoom: 16 +interface MapViewport { + position: [number, number]; + zoom: number; +} + +export const initialMapViewport: MapViewport = { + position: [51.5245255, -0.1338422], // lat,lng + zoom: 16, }; export type MapTheme = 'light' | 'night'; diff --git a/app/src/frontend/map/map.tsx b/app/src/frontend/map/map.tsx index 82af9e33..14b5c42c 100644 --- a/app/src/frontend/map/map.tsx +++ b/app/src/frontend/map/map.tsx @@ -8,7 +8,7 @@ import { apiGet } from '../apiHelpers'; import { HelpIcon } from '../components/icons'; import { categoryMapsConfig } from '../config/category-maps-config'; import { Category } from '../config/categories-config'; -import { defaultMapPosition, mapBackgroundColor, MapTheme } from '../config/map-config'; +import { initialMapViewport, mapBackgroundColor, MapTheme } from '../config/map-config'; import { Building } from '../models/building'; import { CityBaseMapLayer } from './layers/city-base-map-layer'; @@ -32,8 +32,7 @@ interface ColouringMapProps { interface ColouringMapState { theme: MapTheme; - lat: number; - lng: number; + position: [number, number]; zoom: number; } @@ -45,17 +44,16 @@ class ColouringMap extends Component { super(props); this.state = { theme: 'light', - ...defaultMapPosition + ...initialMapViewport }; this.handleClick = this.handleClick.bind(this); this.handleLocate = this.handleLocate.bind(this); this.themeSwitch = this.themeSwitch.bind(this); } - handleLocate(lat, lng, zoom){ + handleLocate(lat: number, lng: number, zoom: number){ this.setState({ - lat: lat, - lng: lng, + position: [lat, lng], zoom: zoom }); } @@ -78,8 +76,6 @@ class ColouringMap extends Component { render() { const categoryMapDefinition = categoryMapsConfig[this.props.category]; - const position: [number, number] = [this.state.lat, this.state.lng]; - const tileset = categoryMapDefinition.mapStyle; const hasSelection = this.props.selectedBuildingId != undefined; @@ -88,8 +84,8 @@ class ColouringMap extends Component { return (
{ > + { + map.setView(position, zoom) + }, [position, zoom]); + + return null; +} + export default ColouringMap;