React-Gmaps-Utilsは、Googleマップ機能を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 >
)
}コンポーネントには、Autocompleteが提案を取得するのを防ぐために渡すことができるブール値であるshouldFetchもあります。 USECaseの例は、ユーザーの相互作用がある場合にのみフェッチを作成したい場合に可能です。
ReverseGeocodeByLocationコンポーネントは、指定された場所に基づいて逆ジオコディングを実行します。
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