react gmaps utils
v0.1.8
React-Gmaps-Utils是一个React库,可提供用于将Google Maps功能集成到您的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这是一个布尔值,您可以通过,以防止自动完成提出建议。示例用户酶可能是只有在存在用户互动时才能进行获取。
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 Maps的API密钥。确保将"YOUR_API_KEY"替换为GoogleMapsProvider组件中的实际API键。
麻省理工学院©Abdelrahman146