react-google-maps-api icon indicating copy to clipboard operation
react-google-maps-api copied to clipboard

Map jumps/recenters whenever InfoWindow is added (dynamically)

Open eggerand13 opened this issue 5 years ago • 3 comments

Issue template

If you want to ask question, ask it in Github Discussions, Spectrum.chat or Slack channel

Please do not post unformatted code into issues, and please do not ask questions. Only real issues, PR or feature requests allowed. Minimal reproduction in codesandbox.io is must have.

Please provide an explanation

Your Environment

os: windows

node --version v14.15.1

react version 16.13.1

webpack version

@babel version

@react-google-maps/api version 2.1.1

How is it behave?

When the user hovers over the polygon, the map automatically jumps to the initial center position no matter where you have been before or where the added InfoWindow appears.

How should it behave correctly?

The InfoWindow should appear when the user hovers over the polygon and the map should not move at all (when the disableAutoPan option is true) or move only to make the InfoWindow visible.

I have implemented the same feature in the past on a Vanilla JS website with the standard google maps library and it worked. Just in the React version I can't seem to get it to work properly.

basic implementation of incorrect behavior in codesandbox.com

I created a very simple and barebones Codesandbox that should be easy to understand. https://codesandbox.io/s/react-google-maps-api-forked-55v7s

Just move the map a little and then hover over the polygon.

By the way: The behaviour does not depend on the position of the polygon or the InfoWindow. I just set them close to the center for easier usage in the example. You can set both far away from the center and it'll still jump to the initial center when it is hovered.

I tried so many different approaches but the behaviour is always the same. I have no idea why this error happens.

I would be very glad if you could help me with this problem.

eggerand13 avatar Mar 25 '21 12:03 eggerand13

I edited my code example to reflect the tips that @JustFly1984 gave in this issue: https://github.com/JustFly1984/react-google-maps-api/issues/240

The problem still remains since the state change (to toggle the InfoWindow) will obviously cause a rerender of the map. What is the reason that a rerender will only recenter the map but still persist all other changes on the map? Simply not recentering the map on every rerender would fix this problem. How would I be able to achieve this?

eggerand13 avatar Mar 26 '21 10:03 eggerand13

I think it's because the mapCenter is remade every rerender the object is causing gmaps to move back to there too. Try to move the mapCenter constant out of the component and you won't get that issue.

A few nice ways to deal with that ofc but I'm sure you'll figure out if this is indeed the issue.

rubennaatje avatar Apr 13 '21 14:04 rubennaatje

@rubennaatje It really baffles me how I had missed that. But it makes perfect sense. Since React will recreate the object on every rerender and it can't compare the object to the previous version (or will at least think it is new anyway) it will always assume it is new and rerender gmaps.

Thank you. Issue seems to be closed.

eggerand13 avatar Apr 13 '21 15:04 eggerand13