React-GMAPS-UTILS adalah perpustakaan bereaksi yang menyediakan komponen dan kait untuk mengintegrasikan fungsionalitas Google Maps ke dalam aplikasi React Anda.
Anda dapat menginstal react-gmaps-utils menggunakan NPM:
npm install react-gmaps-utils
npm install --save-dev @types/google.maps Komponen GoogleMapsProvider digunakan untuk memuat skrip Google Maps dan memberikan konteks untuk komponen lain untuk mengakses API Google Maps.
import { GoogleMapsProvider } from 'react-gmaps-utils'
function App ( ) {
return (
< GoogleMapsProvider apiKey = 'YOUR_API_KEY' >
{ /* Your application components */ }
</ GoogleMapsProvider >
)
} Komponen Map membuat Google Map dan menyediakan berbagai opsi untuk kustomisasi.
import { Map } from 'react-gmaps-utils'
function MyMap ( ) {
return (
< Map
options = { {
center : { lat : 37.7749 , lng : - 122.4194 } ,
zoom : 10
} }
>
{ /* Optional child components */ }
</ Map >
)
} Komponen Marker menambahkan penanda ke peta pada posisi yang ditentukan.
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 >
)
} Komponen Autocomplete menyediakan bidang input dengan fungsionalitas AutoComplete untuk tempat.
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 >
)
} Komponen ini juga shouldFetch boolean yang dapat Anda lewati untuk mencegah autocomplete dari mengambil saran. Contoh Usecase bisa jika Anda ingin membuat pengambilan terjadi hanya jika ada interaksi pengguna.
Komponen ReverseGeocodeByLocation melakukan geocoding terbalik berdasarkan lokasi yang ditentukan.
import { ReverseGeocodeByLocation } from 'react-gmaps-utils'
function MyReverseGeocoder ( ) {
return (
< ReverseGeocodeByLocation
position = { { lat : 37.7749 , lng : - 122.4194 } }
onAddressReceived = { ( result ) => console . log ( result ) }
/>
)
} Komponen MapEvent mendengarkan acara di peta dan menjalankan fungsi panggilan balik ketika acara dipicu.
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 >
)
} Hook useCurrentPosition mengambil posisi pengguna saat ini.
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 >
)
} Perpustakaan ini membutuhkan kunci API dari Google Maps. Pastikan untuk mengganti "YOUR_API_KEY" dengan kunci API Anda yang sebenarnya di komponen GoogleMapsProvider .
MIT © Abdelrahman146