-
Notifications
You must be signed in to change notification settings - Fork 935
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
how to set bounds? #305
Comments
I too am still having trouble with this. I can't see it on the refs for some reason. |
@tomchentw I'm assuming that the open source community and yourself are no longer maintaining this library? It's been a few days since I've heard from anyone on these issues. I'm referencing the ref that is set on my google map component and I have no access to any of the methods claimed to be on the refs. Please advise. You're library is the better implementation out there so I'd hate to have to go to a different one in order to have access to some of the built in GMaps methods. |
@ryanhca I've had the same issue with the map centering around markers being added. |
I think @tomchentw is no longer maintaining this since there's a message in the README that he's looking for other maintainers. |
I found myself able to access the Map instance by throwing a ref on my component and then accessing the map throwing this.refs.whateverYouCalledIt.state.map I just kind of hacked my way around, not sure if its the best implementation but it might help. |
@KensoDev The link you shared takes me to a forked version of this project, I do notice you have a bunch of branches in this fork, but I'm curious as to which files you have an example for me in. I don't want to have to poke around the entire app to get what you're talking about. Thanks! |
@evfleet That didn't work for me? Where are you throwing "this.refs.foobar.state.map"? in the ComponentDidMount? Render? or any other lifecycle method? |
I did it in the render method. Further up in the script, "bounds" is initialized, then extended to contain the Marker's center, and finally extended to include the Polygon's path. This piece borrows heavily from the front page (Readme) sample, but you can see where I set the map object's bounds once the ref is instantiated (right after logging the object to the console).
|
This is what I have so far... and I'm getting an error because I believe I have a race condition |
Use the Also, 6.0.0 is released on npm beta tag now. We also have a new demo page. Feel free to try it: |
Not sure if this is useful to anyone, but in case someone is looking for the same thing, I found a way to make the map 'fit' whatever container you put it in with a little Javascript hacking.
I'd post more code but it's proprietary :/ |
Please refer to Getting Help section in the README (or #469). |
did anyone manage to get a working example for using fitBounds with react-google-maps? |
also looking for a way to do this, could there be an example added to the docs? |
I did it this way, hope it helps. <GoogleMap ref='map'>{markers}</GoogleMap> and in componentDidUpdate const bounds = new window.google.maps.LatLngBounds()
this.props.hotels.map((hotel, i) => {
bounds.extend(new window.google.maps.LatLng(
hotel.attributes.location.lat,
hotel.attributes.location.lng
));
});
this.refs.map.fitBounds(bounds) |
Thanks @jtruzzi – although I put it all in the const bounds = new window.google.maps.LatLngBounds();
const coordinates = this.props.locations.map(model => {
const { latitude, longitude } = model.particulars.location.point;
const latLng = new window.google.maps.LatLng(latitude, longitude);
bounds.extend(latLng);
return latLng;
});
return (
<GoogleMap
ref={map => map && map.fitBounds(bounds)}
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{coordinates.map(({ lat, lng }) => {
return <Marker key={hash(lat, lng)} position={{ lat: lat(), lng: lng() }} />;
})}
</GoogleMap>
); |
The example demonstrates how to center viewport for the given markers:
|
this is only helpful if you use Recompose. |
FYI to those who didn't know: there is a onTilesLoaded property on GoogleMap, meaning with a flag, you can have a handler for when the map is first loaded. You need a flag or it will prevent you from panning, since new tiles are always loaded. I'm not sure if this is the best way to do it but it was pretty straightforward anyway. |
I get bounds from server api. For example:
{
ne: { lat: 52.6754542, lng: 13.7611175 },
sw: { lat: 52.33962959999999, lng: 13.0891554 },
}
Google Api has fitBounds() method. Unfortunately, I don't understand how do it via this component.
Can I set bounds to map component?
Thx for help
The text was updated successfully, but these errors were encountered: