@react-google-maps/api
@ react-google-maps/api
You displace donate operating room become adenine sponsor of the project here : hypertext transfer protocol : //opencollective.com/react-google-maps-api # category-CONTRIBUTE
Reading: @react-google-maps/api
This library ask react v16.6 oregon later. To use the late sport ( include hook ) necessitate react v16.8+. If you want support for early version of react, you should check extinct react-google-maps
This be accomplished re-write of the ( sadly unmaintained )
react-google-maps
library. We thank tomchentw for his great work that make potential .
@ react-google-maps/api provide identical bare dressing to the google function api and let you use information technology in your app equally react component .
here be the independent addition to react-google-maps that be the motivation behind this re-writeInstall @react-google-maps/api
with NPM
npm i -S @react-google-maps/api
operating room yarn
yarn add @react-google-maps/api
import react from 'react ' consequence { GoogleMap, useJsApiLoader } from ' @ react-google-maps/api ' ; const containerStyle = { width: '400px ' , stature: '400px ' } ; const center = { latissimus dorsi: - 3.745 , lng: - 38.523 } ; function MyComponent ( ) { const { isLoaded } = useJsApiLoader ( { id: 'google-map-script ' , googleMapsApiKey: `` YOUR_API_KEY '' } ) const [ map, setMap ] = react. useState ( nothing ) const onLoad = react. useCallback ( serve recall ( map ) { // This equal just associate in nursing example of experience and use the map exemplify ! ! ! dress n't just blindly copy ! const jump = new window. google. map. LatLngBounds ( plaza ) ; map. fitBounds ( restrict ) ; setMap ( map ) }, [ ] ) const onUnmount = react. useCallback ( function recall ( map ) { setMap ( null ) }, [ ] ) return isLoaded? ( < GoogleMap mapContainerStyle = { containerStyle } center = { center } zoom = { ten } onLoad = { onLoad } onUnmount = { onUnmount } > { / * child component, such arsenic marker, information window, etc. * / } < > < / > < / GoogleMap > ) : < > < / > } export default react. memo ( MyComponent )Read more : JavaScript - Wikipedia
Migration from [email protected]
if you want associate in nursing access to map object, rather of
ref
prop, you motivation to useonLoad
recall onpart .
earlier :// ahead - do n't do this ! < GoogleMap referee = { function = > { const bounce = new windowpane. google. function. LatLngBounds ( ) ; map. fitBounds ( limit ) ; } } / >subsequently :
< GoogleMap onLoad = { map = > { const boundary = new window. google. map. LatLngBounds ( ) ; map. fitBounds ( bounce ) ; } } onUnmount = { map = > { // practice your thrust ahead map be unmounted } } / >If you want to use
window.google
object, you need to excerpt GoogleMap in separate faculty, thus information technology equal faineant perform thengoogle-maps-api
handwriting be laden and run by. If you sample to use
window.google
earlier information technology embody loaded information technology will be undefined and you ‘ll induce ampere TypeError .Main features
- Simplified API
- Uses the new Context API
- Supports async React (StrictMode compliant)
- Removes lodash dependency =>
smaller bundle size12.4kb
gzip, tree-shakeable https://bundlephobia.com/result?p=@react-google-maps/api- forbids loading of Roboto fonts, if you set property preventGoogleFonts on
component
Examples
example displace be find in deuce place :
- Official docs (powered by react-styleguidist.
- A Gatsby app including some examples. See the examples folder
- Gatsby.js Demo
Advice
use the exercise command you to generate ampere google map api key. For instruction on how to make that please examine the be guide
Community Help Resource
You can join the community astatine Spectrum.chat to ask motion and aid others with your experience operating room join our slack distribution channel
Contribute
upholder and contributor exist very welcome ! see this issue to drive start .
How to test changes locally
When cultivate on a feature/fix, you ‘re probably gon sodium lack to test your switch. This work flow be a influence in progress. please feel free to better information technology !
- In the file
packages/react-google-maps-api/package.json
changemain
to"src/index.ts"
- In the same file, delete the
module
field- You can now use the package
react-google-maps-api-gatsby-example
to test your changes. Just make sure you change the import from@react-google-maps/api
to../../../react-google-maps-api
Since 1.2.0 you toilet consumption onLoad and onMount prop for each @ react-google-maps/api component, referee do not control API method acting anymore .
Since translation 1.2.2 We total useGoogleMap pilfer, which cost working only with react @ 16.8.1 and late version .Websites made with @react-google-maps-api
DriveFromTo.com transportation reserve service PWA.
Shipwrecks.cc shipwreck from Wikipedia visualize on the map ( Github )
nycmesh.net network topography visualize on the map ( Github )
total your web site by make puerto rico !