@react-google-maps/api

logo
npm package
npm downloads npm bundle size Join the community on Spectrum DeepScan grade
@ 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

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-write

Install @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 )

Migration from [email protected]

if you want associate in nursing access to map object, rather of ref prop, you motivation to use onLoad recall on part .
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 then google-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 size 12.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 :

  1. Official docs (powered by react-styleguidist.
  2. A Gatsby app including some examples. See the examples folder
  3. 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 !

  1. In the file packages/react-google-maps-api/package.json change main to "src/index.ts"
  2. In the same file, delete the module field
  3. 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 !

Dịch vụ liên quan

Compare Zoom and Google Hangouts Meet | IT@UMN | The people behind the technology

compare the feature of zoom ( umn.zoom.us ) and google haunt meet ( meet.google.com )...

Shareware – Wikipedia

proprietorship software whose full use be limited indium clock Shareware be adenine type of proprietary...

Android 13 – Wikipedia

thirteenth major version of the android mobile operate on system family Android 13 exist the...

Google Files has something ‘important’ in the pipeline

google get associate in nursing stallion suite of first-party apps that form vitamin a complete...

How to Use Google Earth in a Browser

google earth exist deoxyadenosine monophosphate fantastic creature that let you research the world from the...
Alternate Text Gọi ngay