Question

Previously I followed the instructions in another answer to get the infowindow to automatically open when there is just a single point on the map. I have this working but the infowindow is slightly cut off at the top since the map doesn't recenter when opening up the infowindow manually (like it does when you click the marker to show the infowindow). My view just uses the basic:

gmaps(:map_options => { :auto_adjust => true },
      :markers => { :data => @json, :options => {:do_clustering => true} })

And the following JavaScript in my view:

<script type="text/javascript" charset="utf-8">
  Gmaps.map.callback = function() {
    if (Gmaps.map.markers.length == 1) {
      var marker = Gmaps.map.markers[0];
      var infowindow = marker.infowindow;
      infowindow.open(Gmaps.map.map, marker.serviceObject);
    }
  }
</script>

What I'm trying to figure out is the best way to both set the zoom level to 14 as well as well as recenter the marker so the entire infowindow shows onscreen and isn't cut off at the top.

I've also looked at this question who seems to be having a similar issue but doesn't include recentering. I too can execute Gmaps.map.map.setZoom(14); within console and have it execute as well as Gmaps.map.map.panBy(0, -80); but they don't seem to execute from within the callback. Plus I'm not sure if panBy is the most effect way to address my infowindow being cut off problem.

Using: gmaps4rails (1.4.8) and Rails (3.2.3)

Anyone have any thoughts or ideas?

Thanks!

Was it helpful?

Solution

As we finally conclude, you should use dedicated helper options when you have a single marker.

Due to google maps async loading.

OTHER TIPS

Maybe because Gmaps.map.map is deprecated in favor of Gmaps.map.serviceObject (see https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Javascript-goodies )

I had got the similar issue in my application, but I did the following to solve.

Try it

1) change the map_option "auto_adjust " to false as follows

gmaps(:map_options => { :auto_adjust => false }, :markers => { :data => @json, :options => {:do_clustering => true} })

2) Now call map.setZoom(your value) in call back function then the map will be zoomed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top