question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't set zoom or initial center on GoogleMap component.

See original GitHub issue

I updated the package and applied the method in the example on the NPM doc, setting center and zoom props but they don’t seem to have any effect.

   <GoogleMap
        mapContainerStyle={containerStyle}
        center={{
          lat: 46.13033,
          lng: -90.346771,
        }}
        zoom={1}
        options={{
          styles: isDark ? darkMapStyle : lightMapStyle,
        }}
        onLoad={onLoad}
        onUnmount={onUnmount}
    ></GoogleMap>

Your Environment

os: windows

node --version

react version 16.13.1

webpack version

@babel version

@react-google-maps/api version 1.13.0

How is it behave?

initial center is in another coordinate, and zoom is not the zoom I set.

How should it behave correctly?

Map should have the center position entered, and the zoom.

basic implementation of incorrect behavior in codesandbox.com

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

27reactions
joobaccacommented, Apr 23, 2021

As @candrescastel mentioned already, you should check your onLoad function! I had the same issue because I copy pasted the whole Maps component at first.

const onLoad = React.useCallback(function callback(map) {
  const bounds = new window.google.maps.LatLngBounds();
  map.fitBounds(bounds); // Omit this line
  setMap(map);
}, []);
8reactions
CamTheWebGuycommented, Feb 2, 2021

As @candrescastel mentioned already, you should check your onLoad function! I had the same issue because I copy pasted the whole Maps component at first.

const onLoad = React.useCallback(function callback(map) {
  const bounds = new window.google.maps.LatLngBounds();
  map.fitBounds(bounds); // Omit this line
  setMap(map)
}, [])

This is the answer OP. I had the same issue, this is how I fixed it. Thank you @joobacca

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't change center of the map #579 - GitHub
Same, can't change center of map on state or props update. Zoom works, though. This can't be a react component if the doesn't...
Read more >
google maps api initial zoom error - Stack Overflow
If you only have one marker, don't use fitBounds, use .setCenter (on the coordinates of the marker, .setZoom (to your desired zoom level)....
Read more >
Controlling Zoom and Pan | Maps JavaScript API
The following map demonstrates the default behavior for map interactions with a map instantiated with only the zoom and center options defined.
Read more >
A Beginner's Guide to google-map-react | by Matt Croak Code
I still want to maintain the original defaultZoom (the user can zoom in and out by clicking the plus and minus symbols on...
Read more >
React Google Maps Style Guide
Everything inside a component will be mounted automatically on the map And ... or center and zoom have not been set then the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found