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.

Invoking a fitBounds on initialization

See original GitHub issue

How to execute a method fitBounds once the map object initialized?

Where am I so far (terribly wrong, but most of the time works):

  render() {
    var center = [59.938043, 30.337157];
    var zoom = 8;

    return (
      <div className="search-map">
        <GoogleMap
          center={center}
          zoom={zoom}
          ref="googleMap">
          {this.renderMarkers()}
        </GoogleMap>
      </div>
    );
  },
  componentDidUpdate() {
    var map = this.refs.googleMap.map_;
    if (map) {
      var maps = this.refs.googleMap.maps_;
      var bounds = new maps.LatLngBounds();
      each(this.props.places, (place) => {
        var latLng = new maps.LatLng(place.lat, place.lng);
        bounds.extend(latLng);
      });
      map.fitBounds(bounds);
    }
  }

Most of the time it works, but sometimes an update happens before the map mounted, or in other words googleMapLoader is resolved.

I believe the correct solution is to invoke props callback in componentDidMount GoogleMap component.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
yrikcommented, May 7, 2016

What is the best way to get nw, se from set of markers?

15reactions
istarkovcommented, Apr 14, 2017

@voold No! have your read this? Fast and dirty realization and please no questions about as I will not answer

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Maps map.getBounds() immediately after a call to map ...
The nearest I get is a note on getBounds that says: If the map is not yet initialized (i.e. the mapType is still...
Read more >
Fit a map to a bounding box | Mapbox GL JS
... fitBounds() pans and zooms the map viewport from the starting location on the city of Mombasa so that the new visible area...
Read more >
Leaflet fitBounds Starting Position - GIS Stack Exchange
The issue I have is that I want to set a start position that isn't the bounds of all of my markers. I...
Read more >
Fit bounds after opening infoWindow - Google Groups
So, I'm adding markers to the map and after adding them I open the infoWindow attached to the first marker added with a...
Read more >
Maps JavaScript API | Google Developers
Note: When the map is set to display: none , the fitBounds function reads the ... If the map is not yet initialized...
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