Skip to content

Commit f52e404

Browse files
committed
feat : geolocation 기준 distance 렌더링 구현 (#36)
1 parent 4731be6 commit f52e404

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

src/atoms/geolocation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { atom } from 'recoil';
2+
3+
export const GeolocationAtom = atom({
4+
key: 'geolocation',
5+
default: {
6+
latitude: 37.555,
7+
longitude: 126.91,
8+
},
9+
});

src/components/PlaceSearch/PlaceSearchList.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ const PlaceSearchList: FC<IPlaceSearchListProps> = (props) => {
1212
const { places } = props;
1313
return (
1414
<List>
15-
{places.map(({ id, place_name }) => (
16-
<PlaceSearchListItem key={id} placeName={place_name} />
15+
{places.map(({ id, place_name, distance }) => (
16+
<PlaceSearchListItem
17+
key={id}
18+
placeName={place_name}
19+
distance={distance}
20+
/>
1721
))}
1822
</List>
1923
);

src/components/PlaceSearch/PlaceSearchListItem.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import React, { FC } from 'react';
22
import styled from 'styled-components';
33
import { useStoryState } from '@/atoms/storyState';
44
import { useHistory } from 'react-router-dom';
5+
import { getFormattedDistance } from '@/utils/formatter';
56

67
interface IPlaceSearchListItem {
78
placeName: string;
9+
distance: string;
810
}
911

1012
const PlaceSearchListItem: FC<IPlaceSearchListItem> = (props) => {
11-
const { placeName } = props;
13+
const { placeName, distance } = props;
1214
const { setStoryState } = useStoryState();
1315
const history = useHistory();
1416

@@ -20,7 +22,7 @@ const PlaceSearchListItem: FC<IPlaceSearchListItem> = (props) => {
2022
return (
2123
<ListItem onClick={onClickPlaceItem}>
2224
<strong>{placeName}</strong>
23-
<span>940m</span>
25+
<span>{getFormattedDistance(distance)}</span>
2426
</ListItem>
2527
);
2628
};

src/components/PlaceSearch/index.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/* global kakao */
21
import React, { useEffect, useState } from 'react';
2+
import { useRecoilState } from 'recoil';
3+
import { GeolocationAtom } from '@/atoms/geolocation';
34
import Layout from '@/components/Layout';
45
import SearchHeader from '@/components/SearchHeader';
56
import SearchInput from '@/components/SearchInput';
@@ -16,15 +17,30 @@ declare global {
1617

1718
const PlaceSearch = () => {
1819
const [placeList, setPlaceList] = useState<IPlace[]>([]);
20+
const [geolocation, setGeolocation] = useRecoilState(GeolocationAtom);
1921
const { value, onChangeValue } = useInput();
2022

2123
const searchPlaceCb = (data: IPlace[], status: string) => {
2224
if (status === 'OK') setPlaceList(data);
2325
};
2426

27+
const getSearchOption = () => ({
28+
x: geolocation.longitude,
29+
y: geolocation.latitude,
30+
});
31+
32+
useEffect(() => {
33+
if (navigator.geolocation) {
34+
navigator.geolocation.getCurrentPosition(({ coords }) => {
35+
const { latitude, longitude } = coords;
36+
setGeolocation({ latitude, longitude });
37+
});
38+
}
39+
}, [setGeolocation]);
40+
2541
useEffect(() => {
2642
const ps = new window.kakao.maps.services.Places();
27-
ps.keywordSearch(value, searchPlaceCb);
43+
if (value.trim()) ps.keywordSearch(value, searchPlaceCb, getSearchOption());
2844
}, [value]);
2945

3046
return (

0 commit comments

Comments
 (0)