Google-map-react: Can't dynamically adjust map zoom level to display all markers

Created on 31 Mar 2017  路  3Comments  路  Source: google-map-react/google-map-react

Is there no way to do this. Do I need to write custom code to determine the zoom level based upon the distance between my map markers?

Most helpful comment

What the problem to calculate nw se of multiple points? It's the same as finding min max of array, are you developers or what?

All 3 comments

read docs, use search.

@zander312 I was running into this same issue but couldn't find it in the docs or any direct answer in the issues so I just used the internal google api:

...

_onMapLoaded({map, maps}) {
  const bounds = new google.maps.LatLngBounds();

  this.props.sites
    .filter(site => site.latitude !== undefined && site.longitude !== undefined)
    .forEach(site => {
      bounds.extend(new google.maps.LatLng(site.latitude, site.longitude));
    });

  map.fitBounds(bounds);
}

render() {

  ...

  return (
    <GoogleMapReact
      ...
      onGoogleApiLoaded={this._onMapLoaded}
      >
      {points}
      </GoogleMapReact>
  );
}

...

@istarkov maybe it could be more apparent somewhere how the component is fit to handle this? I saw the fitBounds utility function but it seems like you need to know the ne/sw and the size of the map itself in order to make use of it which doesn't work in my dynamic points / map size implementation.

What the problem to calculate nw se of multiple points? It's the same as finding min max of array, are you developers or what?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Pau1fitz picture Pau1fitz  路  4Comments

colinwitkamp picture colinwitkamp  路  4Comments

SachaG picture SachaG  路  4Comments

robertwt7 picture robertwt7  路  4Comments

nhducit picture nhducit  路  5Comments