-
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
Auto center based on marker's location #260
Comments
I've done this just by having a property in state called |
Helpful note here, which caused me to spend almost 1 hour in debugging: there are props Hope this helps someone save some time in the future. |
Thanks for the advice guys - I guess I was inquiring on if I have a number of markers, I'd like to set |
@mmahalwy Have you looked at the |
We're working on this to make it clearer. We're also looking for maintainers. Involve in #266 to help strengthen our community! |
This was closed, but was there ever an implementation into the library? It would save me a whole lot of time if someone had an example of how they dropped this functionality into the library. |
@ryanhca Are you just looking for the |
@Dyyylan thanks. I'll give it a whirl. EDIT: After digging in further, I cannot seem to find the method. I've tried binding the component to a ref="map" and console.log(this.refs.map) shows no sign of the method. Did this get updated? |
@Dyyylan I have same question |
It's important to note that since the actual map is mounted asynchronously from the react component, you may not have methods available as soon as it mounts. So it depends on your use case for when you want to call the method, if you already have all your data when the map loads up, you can add a callback on the GoogleMap component with Here's an example that I just tested using the most recent version: |
Can confirm @Dyyylan's method works (thanks!), though keep in mind
|
Wow, this worked really well for me! Thank you. I'm now just diggin in to see if I can force the map to fit bounds, but also zoom out a little. The fit bounds sort of force it to zoom in too far. |
following up. I used @agilgur5 's solution above to only call my set bounds handler once. Now my issue is that the bounds are too far zoomed in if the coords all exist in or near the same place. I'm looking to expose the "setZoom" method on the "map" ref. I can use "getZoom" but not setZoom for some reason. I'm digging in deeper Edit/Solution after my setBoundsHandler is done doing what it's doing, I set the state of a zoom property to whatever number I want. And on the short snippet:
|
There is a difference between If you want to control the zoom dynamically, just use the Most (in my experience anyway) of the |
@ryanhca I am working on a similar problem for fitting map view in bounds, but I am getting inconsistent results. Do you have sample code of the component you can share by any chance? Thanks! |
@Andriy-Kulak So I had this working really well, then something broke my implementation ... IDK if it was the Google api being updated or some faulty logic on my end. But I've tabled a fix for this for a little bit until I have some time to go back to it. I'll let you know what comes of it when/if I get it re-implemented. For now I updated my code snippet above to show you what I did for the zoom. I just set the zoom to a state property and then onTilesLoaded calls the method I use to fit the bounds. Within that method I check to see what the zoom is defaulting too with the new bounds and setState if its zoomed in too far . |
@ryanhca I ended up doing to a workaround for fitbounds. the google maps geometry api was also giving me trouble so I decided to take a different approach. I have two points on a map so all I need to do is to make sure the map is centered in between the two points and the zoom is appropriate. I calculated the middle point between two points and used that a defaultCenter using geoLib npm package (geoLib.getCenter(...) . This package can actually calculate mid-point between multiple coordinates so it works well for 2+ points as well. Then, I calculated the distance from one point to another and used that to proportionally increase/decrease my defaultZoom... using geoLib.getDistance... or you can use Pythagorean theory most people learned in high school geometry class. |
@Andriy-Kulak my gist above is a complete component with fitBounds, if you needed an example of how to use it. |
I'll note that on my solution, I actually added a line at the end,
As I didn't change the state in my callback, it doesn't cause the React part of the view to rerender, and therefore the binding on the component There are likely a few alternatives to this. Also note that this is only necessary if you want it to fire exactly once. That new line isn't on production yet, but you can see this working/in-action here (click the "Map" button) on Yorango's Map View. |
The best answer I have found is:
But how to do this with this library?
The text was updated successfully, but these errors were encountered: