In gmaps4rails, how do I move the map so the marke

2019-07-27 04:27发布

问题:

I have a set of locations that I want to bring up, individually through ajax calls, and some of them aren't within the current bounds of the map. Is there a way to move the map so the marker is in view?

I don't need it to be in the center, just as long as it's in view.

回答1:

I figured it out after some research. I'd still be curious to know how to NOT have to center if the marker is visible on the map.

var centerpoint = new google.maps.LatLng(lat_value, long_value);
Gmaps4Rails.map.setCenter(centerpoint)

edit: Found answer to how to not center map every time. Pseudo code version.
1. Get values from Gmaps4Rails.map.getBounds().
2. Use resulting ta and la values to see if the marker is within those values.
3. If marker is outside of those values, center map, otherwise place marker without centering map.

Thanks guys. I should post more questions. It really helps me to think through my problems.



回答2:

Jim's answer was probably right when he posted it. But as of now the right way to do it would be :

var centerpoint = new google.maps.LatLng(lat_value, long_value);
Gmaps.map.map.setCenter(centerpoint);


回答3:

What you expect is automatically done as long as you pass the auto_adjust setting to true. See here.

Then, you should just use the js function Gmaps4Rails.add_markers described here.