Merge pull request #674 from mz8i/fix/postcode-search

Update map position setting for react-leaflet v3
This commit is contained in:
Maciej Ziarkowski 2021-06-12 01:39:51 +01:00 committed by GitHub
commit 8ea5c077c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 15 deletions

View File

@ -1,7 +1,11 @@
export const defaultMapPosition = { interface MapViewport {
lat: 51.5245255, position: [number, number];
lng: -0.1338422, zoom: number;
zoom: 16 }
export const initialMapViewport: MapViewport = {
position: [51.5245255, -0.1338422], // lat,lng
zoom: 16,
}; };
export type MapTheme = 'light' | 'night'; export type MapTheme = 'light' | 'night';

View File

@ -8,7 +8,7 @@ import { apiGet } from '../apiHelpers';
import { HelpIcon } from '../components/icons'; import { HelpIcon } from '../components/icons';
import { categoryMapsConfig } from '../config/category-maps-config'; import { categoryMapsConfig } from '../config/category-maps-config';
import { Category } from '../config/categories-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 { Building } from '../models/building';
import { CityBaseMapLayer } from './layers/city-base-map-layer'; import { CityBaseMapLayer } from './layers/city-base-map-layer';
@ -32,8 +32,7 @@ interface ColouringMapProps {
interface ColouringMapState { interface ColouringMapState {
theme: MapTheme; theme: MapTheme;
lat: number; position: [number, number];
lng: number;
zoom: number; zoom: number;
} }
@ -45,17 +44,16 @@ class ColouringMap extends Component<ColouringMapProps, ColouringMapState> {
super(props); super(props);
this.state = { this.state = {
theme: 'light', theme: 'light',
...defaultMapPosition ...initialMapViewport
}; };
this.handleClick = this.handleClick.bind(this); this.handleClick = this.handleClick.bind(this);
this.handleLocate = this.handleLocate.bind(this); this.handleLocate = this.handleLocate.bind(this);
this.themeSwitch = this.themeSwitch.bind(this); this.themeSwitch = this.themeSwitch.bind(this);
} }
handleLocate(lat, lng, zoom){ handleLocate(lat: number, lng: number, zoom: number){
this.setState({ this.setState({
lat: lat, position: [lat, lng],
lng: lng,
zoom: zoom zoom: zoom
}); });
} }
@ -78,8 +76,6 @@ class ColouringMap extends Component<ColouringMapProps, ColouringMapState> {
render() { render() {
const categoryMapDefinition = categoryMapsConfig[this.props.category]; const categoryMapDefinition = categoryMapsConfig[this.props.category];
const position: [number, number] = [this.state.lat, this.state.lng];
const tileset = categoryMapDefinition.mapStyle; const tileset = categoryMapDefinition.mapStyle;
const hasSelection = this.props.selectedBuildingId != undefined; const hasSelection = this.props.selectedBuildingId != undefined;
@ -88,8 +84,8 @@ class ColouringMap extends Component<ColouringMapProps, ColouringMapState> {
return ( return (
<div className="map-container"> <div className="map-container">
<MapContainer <MapContainer
center={position} center={initialMapViewport.position}
zoom={this.state.zoom} zoom={initialMapViewport.zoom}
minZoom={9} minZoom={9}
maxZoom={19} maxZoom={19}
doubleClickZoom={false} doubleClickZoom={false}
@ -98,6 +94,7 @@ class ColouringMap extends Component<ColouringMapProps, ColouringMapState> {
> >
<ClickHandler onClick={this.handleClick} /> <ClickHandler onClick={this.handleClick} />
<MapBackgroundColor theme={this.state.theme} /> <MapBackgroundColor theme={this.state.theme} />
<MapViewport position={this.state.position} zoom={this.state.zoom} />
<Pane <Pane
key={this.state.theme} key={this.state.theme}
@ -168,4 +165,20 @@ function MapBackgroundColor({ theme}: {theme: MapTheme}) {
return null; return null;
} }
function MapViewport({
position,
zoom
}: {
position: [number, number];
zoom: number;
}) {
const map = useMap();
useEffect(() => {
map.setView(position, zoom)
}, [position, zoom]);
return null;
}
export default ColouringMap; export default ColouringMap;