In gmaps4rails, how do I move the map so the marker is in view?

拟墨画扇 提交于 2020-01-15 10:42:55

问题


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.



来源:https://stackoverflow.com/questions/6393654/in-gmaps4rails-how-do-i-move-the-map-so-the-marker-is-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!