diff --git a/src/atoms/geolocation.ts b/src/atoms/geolocation.ts new file mode 100644 index 0000000..cf8f41f --- /dev/null +++ b/src/atoms/geolocation.ts @@ -0,0 +1,9 @@ +import { atom } from 'recoil'; + +export const GeolocationAtom = atom({ + key: 'geolocation', + default: { + latitude: 37.555, + longitude: 126.91, + }, +}); diff --git a/src/components/PlaceSearch/PlaceSearchList.tsx b/src/components/PlaceSearch/PlaceSearchList.tsx index 9bde051..d107d66 100644 --- a/src/components/PlaceSearch/PlaceSearchList.tsx +++ b/src/components/PlaceSearch/PlaceSearchList.tsx @@ -12,8 +12,12 @@ const PlaceSearchList: FC = (props) => { const { places } = props; return ( - {places.map(({ id, place_name }) => ( - + {places.map(({ id, place_name, distance }) => ( + ))} ); diff --git a/src/components/PlaceSearch/PlaceSearchListItem.tsx b/src/components/PlaceSearch/PlaceSearchListItem.tsx index f765d3e..c14df79 100644 --- a/src/components/PlaceSearch/PlaceSearchListItem.tsx +++ b/src/components/PlaceSearch/PlaceSearchListItem.tsx @@ -2,13 +2,15 @@ import React, { FC } from 'react'; import styled from 'styled-components'; import { useStoryState } from '@/atoms/storyState'; import { useHistory } from 'react-router-dom'; +import { getFormattedDistance } from '@/utils/formatter'; interface IPlaceSearchListItem { placeName: string; + distance: string; } const PlaceSearchListItem: FC = (props) => { - const { placeName } = props; + const { placeName, distance } = props; const { setStoryState } = useStoryState(); const history = useHistory(); @@ -20,7 +22,7 @@ const PlaceSearchListItem: FC = (props) => { return ( {placeName} - 940m + {getFormattedDistance(distance)} ); }; diff --git a/src/components/PlaceSearch/index.tsx b/src/components/PlaceSearch/index.tsx index d2aba92..252868f 100644 --- a/src/components/PlaceSearch/index.tsx +++ b/src/components/PlaceSearch/index.tsx @@ -1,5 +1,6 @@ -/* global kakao */ import React, { useEffect, useState } from 'react'; +import { useRecoilState } from 'recoil'; +import { GeolocationAtom } from '@/atoms/geolocation'; import Layout from '@/components/Layout'; import SearchHeader from '@/components/SearchHeader'; import SearchInput from '@/components/SearchInput'; @@ -16,15 +17,30 @@ declare global { const PlaceSearch = () => { const [placeList, setPlaceList] = useState([]); + const [geolocation, setGeolocation] = useRecoilState(GeolocationAtom); const { value, onChangeValue } = useInput(); const searchPlaceCb = (data: IPlace[], status: string) => { if (status === 'OK') setPlaceList(data); }; + const getSearchOption = () => ({ + x: geolocation.longitude, + y: geolocation.latitude, + }); + + useEffect(() => { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(({ coords }) => { + const { latitude, longitude } = coords; + setGeolocation({ latitude, longitude }); + }); + } + }, [setGeolocation]); + useEffect(() => { const ps = new window.kakao.maps.services.Places(); - ps.keywordSearch(value, searchPlaceCb); + if (value.trim()) ps.keywordSearch(value, searchPlaceCb, getSearchOption()); }, [value]); return (