React-GMAPS-UTILS는 Google Maps 기능을 React 응용 프로그램에 통합하기위한 구성 요소 및 후크를 제공하는 React 라이브러리입니다.
NPM을 사용하여 React-GMAPS-utils를 설치할 수 있습니다.
npm install react-gmaps-utils
npm install --save-dev @types/google.maps GoogleMapsProvider 구성 요소는 Google Maps 스크립트를로드하고 다른 구성 요소가 Google Maps API에 액세스 할 수있는 컨텍스트를 제공하는 데 사용됩니다.
import { GoogleMapsProvider } from 'react-gmaps-utils'
function App ( ) {
return (
< GoogleMapsProvider apiKey = 'YOUR_API_KEY' >
{ /* Your application components */ }
</ GoogleMapsProvider >
)
} Map 구성 요소는 Google지도를 렌더링하고 사용자 정의를위한 다양한 옵션을 제공합니다.
import { Map } from 'react-gmaps-utils'
function MyMap ( ) {
return (
< Map
options = { {
center : { lat : 37.7749 , lng : - 122.4194 } ,
zoom : 10
} }
>
{ /* Optional child components */ }
</ Map >
)
} Marker 구성 요소는 지정된 위치에서 맵에 마커를 추가합니다.
import { Map } from 'react-gmaps-utils'
function MyMapWithMarker ( ) {
return (
< Map
options = { {
center : { lat : 37.7749 , lng : - 122.4194 } ,
zoom : 10
} }
>
< Map . Marker position = { { lat : 37.7749 , lng : - 122.4194 } } />
</ Map >
)
} Autocomplete 구성 요소는 장소에 대한 자동 완성 기능이있는 입력 필드를 제공합니다.
import { Places , Autocomplete } from 'react-gmaps-utils'
import { useMemo , useRef , useState } from 'react'
function MyAutocomplete ( ) {
const [ query , setQuery ] = useState ( '' )
const autocompleteRef = useRef < { close : ( ) => void } > ( null )
const placesService = useRef < google . maps . places . PlacesService | null > ( null )
const [ placeId , setPlaceId ] = useState < string | null > ( null ) ;
return (
< Places
onLoaded = { ( places ) => {
placesService . current = places
} }
>
< Autocomplete
as = { CustomInput }
ref = { autocompleteRef }
value = { query }
onChange = { ( e : React . ChangeEvent < HTMLInputElement > ) =>
setQuery ( e . target . value )
}
options = { { types : [ '(cities)' ] } }
className = 'input'
renderResult = { ( predictions ) => {
return (
< div className = 'dropdown' >
{ predictions . map ( ( prediction ) => (
< div
className = 'dropdown-item'
key = { prediction . place_id }
onClick = { ( ) => {
setPlaceId ( prediction . place_id )
autocompleteRef . current ?. close ( )
} }
>
< span > { prediction . description } </ span >
</ div >
) ) }
</ div >
)
} }
/>
< Places . FindPlaceByPlaceId placeId = { placeId } onPlaceReceived = { ( place ) => { console . log ( place ?. geometry ?. location ?. toJSON ( ) ) } } />
</ Places >
)
} 구성 요소는 또한자가 완성이 제안을 가져 오는 것을 방지하기 위해 통과 할 수있는 부울 인 shouldFetch 도 있습니다. USECASE는 사용자 상호 작용이있는 경우에만 페치를 만들고 싶을 경우입니다.
ReverseGeocodeByLocation 구성 요소는 지정된 위치에 따라 Reverse Geocoding을 수행합니다.
import { ReverseGeocodeByLocation } from 'react-gmaps-utils'
function MyReverseGeocoder ( ) {
return (
< ReverseGeocodeByLocation
position = { { lat : 37.7749 , lng : - 122.4194 } }
onAddressReceived = { ( result ) => console . log ( result ) }
/>
)
} MapEvent 구성 요소는 맵의 이벤트에 대해 장려하고 이벤트가 트리거 될 때 콜백 함수를 실행합니다.
import { MapEvent } from 'react-gmaps-utils'
function MyMapEvent ( ) {
const handleMapClick = ( map , event ) => {
console . log ( 'Map clicked at:' , event . latLng . lat ( ) , event . latLng . lng ( ) )
}
return (
< Map
options = { {
center : { lat : 37.7749 , lng : - 122.4194 } ,
zoom : 10
} }
>
< Map . Event event = 'click' callback = { handleMapClick } />
</ Map >
)
} useCurrentPosition 후크는 사용자의 현재 위치를 검색합니다.
import { useCurrentPosition } from 'react-gmaps-utils'
function MyComponent ( ) {
const { position , loading , error , getCurrentPosition , getIPBasedPosition } = useCurrentPosition ( {
onInit : {
getPosition : true ,
ipBased : true ,
}
} )
if ( loading ) return < div > Loading... </ div >
if ( error ) return < div > Error: { error } </ div >
return (
< div >
Latitude: { position ?. lat }
< br />
Longitude: { position ?. lng }
< br />
< button onClick = { getCurrentPosition } > Get Exact Location </ button >
</ div >
)
} 이 라이브러리에는 Google지도에서 API 키가 필요합니다. GoogleMapsProvider 구성 요소의 실제 API 키로 "YOUR_API_KEY" 교체하십시오.
MIT © ABDELRAHMAN146