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

2019-07-27 03:53发布

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.

3条回答
乱世女痞
2楼-- · 2019-07-27 04:16

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.

查看更多
狗以群分
3楼-- · 2019-07-27 04:28

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-07-27 04:37

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);
查看更多
登录 后发表回答