Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сохраняет последнее положение карты, обновляет координаты при сдвигах #191

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,33 @@ const ACCESS_TOKEN = 'pk.eyJ1IjoieXVsaWEtYXZkZWV2YSIsImEiOiJjazh0enUyOGEwNTR1M29
const PAGES_RESOURCE = '/_api/heritage/?action=search&format=json&limit=5000&srcountry=ru&&props=id|name|address|municipality|lat|lon|image|source&bbox=';
const MIN_ZOOM_LEVEL = 0;

class MyMap extends Component<MapPropsInterface> {
state: MyMapParams = {
viewport: {
latitude: 55.7522,
longitude: 37.6155,
zoom: 10,
bearing: 0,
pitch: 0,
width: undefined,
height: undefined,
},
searchValue: '',
monuments: [],
loading: false,
};
class MyMap extends Component<MapPropsInterface, MyMapParams> {
constructor(props: MapPropsInterface) {
super(props);

let prevPosition: { lat?: string, lon?: string } = {};

try {
prevPosition = JSON.parse(window.localStorage.getItem('viewport') || '');
} catch (err) {
console.log(err);
}

this.state = {
viewport: {
latitude: prevPosition?.lat || 55.7522,
longitude: prevPosition?.lon || 37.6155,
zoom: 10,
bearing: 0,
pitch: 0,
width: undefined,
height: undefined,
},
searchValue: '',
monuments: [],
loading: false,
};
}

abortController: { abort: () => void; signal: any } | undefined = undefined;

Expand Down Expand Up @@ -92,20 +104,22 @@ class MyMap extends Component<MapPropsInterface> {
const width = document.body.offsetWidth;
const height = document.body.offsetHeight;
const { longitude, latitude, zoom } = viewport;
const maxZoom = Math.max(MIN_ZOOM_LEVEL, Number(zoom));

const bbox = getBbox({
longitude,
latitude,
width,
height,
zoom: Math.max(MIN_ZOOM_LEVEL, Number(zoom)),
zoom: maxZoom,
});

// eslint-disable-next-line react/no-unused-state
this.setState({ viewport, zoom: Math.max(MIN_ZOOM_LEVEL, zoom) });
this.setState((prevState) => ({ viewport: { ...prevState.viewport, zoom: maxZoom } }));

window.localStorage.setItem('viewport', JSON.stringify(viewport));

this.loadPointsWithDebounce(bbox);
this.props.history.push(getRoute({ lat: latitude, lon: longitude }));
};

handleMapLoad = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/Map.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AlertManager } from 'react-alert';

export interface ViewportInterface {
latitude?: number,
longitude?: number,
latitude?: number | string,
longitude?: number | string,
zoom?: number,
bearing?: number,
pitch?: number,
Expand Down