Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-center map if new center property value equals to previous one, but map was re-positioned manually #1100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

undeletable
Copy link

@undeletable undeletable commented Mar 26, 2022

Related to #546
Assume the following code:

import React, {Fragment, useEffect, useState} from 'react';
import GoogleMapReact from 'google-map-react';

const Map = () => {
  const [center, setCenter] = useState();

  useEffect(() => {
    window.navigator.geolocation.getCurrentPosition(position => {
      setCenter({
        lat: position.coords.latitude,
        lng: position.coords.longitude
      });
    });
  }, []);

  return <Fragment>
    <GoogleMapReact
      bootstrapURLKeys={{ key: 'my_api_key' }}
      center={center}
      yesIWantToUseGoogleMapApiInternals
    />
  <button
    onClick={() => window.navigator.geolocation.getCurrentPosition(position => {
      setCenter({
        lat: position.coords.latitude,
        lng: position.coords.longitude
      });
    })}
  >
    Locate Me
  </button>
  </Fragment>;
};

Execute the following steps:

  1. Click 'Locate Me' button - map would be centered to the detected location.
  2. Move the map.
  3. Click 'Locate Me' - map would not be centered, even thougn center prop receives the expected value.

center prop value is not changed (on steps 1 and 3 coordinates are the same), but at the same time center prop value is different from actual center. Such a behavior of component might be confusing. IMHO, when deciding whether to re-center map programmatically, it makes sense to compare center value with current actual center.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant