Skip to content

Commit

Permalink
feat: 검색과 마커 연동 테스트 컴포넌트 #12
Browse files Browse the repository at this point in the history
  • Loading branch information
1119wj committed Nov 5, 2024
1 parent 8681bd9 commit 282ddfe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function App() {
<Wrapper
apiKey={import.meta.env.VITE_GOOGLE_MAPS_API_KEY || ''}
render={render}
libraries={['marker']}
libraries={['marker', 'places']}
/>
);
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from 'react';
import { getGoogleMapStore, useGoogleMapStore } from './store/googleMapState';
import { createMarkerInstance } from './marker';
import NewPlaceSearchBar from './NewPlaceSearchBar';

const Dashboard = () => {
const ref = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -58,6 +59,7 @@ const Dashboard = () => {

return (
<div className={'absolute left-0 top-0 h-72 w-36 bg-white'}>
<NewPlaceSearchBar />
<div>
<label>
Latitude:
Expand Down
48 changes: 48 additions & 0 deletions frontend/src/NewPlaceSearchBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useEffect, useRef } from 'react';
import { useGoogleMapStore } from './store/googleMapState';

function NewPlaceSearchBar() {
const inputRef = useRef(null);
const googleMap = useGoogleMapStore.getState().googleMap;
const markerRef = useRef<google.maps.marker.AdvancedMarkerElement | null>(
null,
);

useEffect(() => {
if (!googleMap || !inputRef.current) return;

const autocomplete = new google.maps.places.Autocomplete(inputRef.current, {
fields: ['place_id', 'geometry', 'name'],
});

autocomplete.addListener('place_changed', () => {
const place = autocomplete.getPlace();
if (!place.geometry || !place.geometry.location) {
alert('장소의 위치 정보를 가져올 수 없습니다.');
return;
}

googleMap.setCenter(place.geometry.location);
googleMap.setZoom(15);

if (markerRef.current) {
markerRef.current.map = null;
}
markerRef.current = new google.maps.marker.AdvancedMarkerElement({
position: place.geometry.location,
map: googleMap,
});
});
}, [googleMap]);

return (
<input
ref={inputRef}
type="text"
placeholder="검색할 장소를 입력하세요"
style={{ width: '300px', padding: '8px' }}
/>
);
}

export default NewPlaceSearchBar;

0 comments on commit 282ddfe

Please sign in to comment.