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

Объекты с одинаковыми координатами нельзя посмотреть #179 #212

Merged
merged 5 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 26 additions & 6 deletions src/components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
MIN_ZOOM_LEVEL,
} from 'constants/map';

import getSortedMonumentsByCoords from 'utils/getSortedMonumentsByCoords';

import MarkerButton from 'components/MarkerButton';

import styles from './MyMap.module.scss';
Expand Down Expand Up @@ -173,7 +175,9 @@ class MyMap extends Component<MapPropsInterface, MyMapParams> {

const { monuments } = await response.json();

this.setState({ monuments: monuments || [] });
this.setState({
monuments: getSortedMonumentsByCoords(monuments),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если monuments - undefined, всё будет ок?

});

if (!monuments || monuments.length === 0) {
this.props.alert.show('Достопримечательности не найдены');
Expand Down Expand Up @@ -232,11 +236,27 @@ class MyMap extends Component<MapPropsInterface, MyMapParams> {
)}
ref={this.cluster}
>
{this.state.monuments.map((item: MonumentInterface) => (
<Marker key={item.id} longitude={item.lon} latitude={item.lat}>
<MarkerButton item={item} />
</Marker>
))}
{this.state.monuments.map((group: MonumentInterface[]) => {
if (group.length > 1) {
return (
group.map((item, index) => (
<Marker
key={item.id}
longitude={item.lon + (index % 3) / 10000}
latitude={item.lat - (Math.floor(index / 3)) / 15000}
>
<MarkerButton item={item} />
</Marker>
))
);
}

return (
<Marker key={group[0].id} longitude={group[0].lon} latitude={group[0].lat}>
avdeev marked this conversation as resolved.
Show resolved Hide resolved
<MarkerButton item={group[0]} />
</Marker>
);
})}
</Cluster>

{this.state.loading && (
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/Map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AlertManager } from 'react-alert';
import MonumentIntarface from './Monument';

export interface ViewportInterface {
latitude?: number | string,
Expand Down Expand Up @@ -27,7 +28,7 @@ export interface MapPropsInterface {
export interface MyMapParams {
viewport: ViewportInterface;
searchValue: string;
monuments: [];
monuments: MonumentIntarface[][];
loading: boolean;
}

Expand Down
21 changes: 21 additions & 0 deletions src/utils/getSortedMonumentsByCoords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import MonumentIntarface from 'interfaces/Monument';

interface AccInterface {
[key: string]: MonumentIntarface[]
}

const getSortedMonumentsByCoords = (monuments: MonumentIntarface[]): MonumentIntarface[][] => {
const sortedMonuments = monuments.reduce((acc: AccInterface, monument: MonumentIntarface) => {
const key = `${monument.lat}_${monument.lon}`;

if (acc[key]) {
return { ...acc, [key]: acc[key].concat(monument) };
}

return { ...acc, [key]: [monument] };
}, {});

return Object.keys(sortedMonuments).map((key) => sortedMonuments[key]);
};

export default getSortedMonumentsByCoords;