1

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!

Community
  • 1
  • 1
Tim Knight
  • 8,346
  • 4
  • 33
  • 32
  • `panBy` is a good solution and it should work in callback, no reason! – apneadiving May 22 '12 at 12:57
  • Thank you apneadiving! I'm half way there it seems. I can get panBy to work from within the callback, but should setZoom work in there as well? Right now I'm calling Gmaps.map.map.setZoom(14); then Gmaps.map.map.panBy(0, -80);. The pan seems to be working now (perhaps a caching issue before) but not the zoom. If I instead pass in the zoom through the options hash the pan doesn't seem to do anything and the map isn't centered at all. Is there another option I should pass in with the zoom into options or should I be calling setZoom differently? – Tim Knight May 22 '12 at 13:11
  • weird, everything in the callback should work fine. I'll take a look. – apneadiving May 22 '12 at 13:15
  • Ok I remember: the map is created async. So because the map isn't finished when `setZoom` is triggered, the zoom level passed on creation prevails... Nothing to do with the gem here. – apneadiving May 22 '12 at 13:30
  • Are you aware of a method that I can call and listen for to see if the map is finished loading? If not, perhaps someone might have a way to offer assistance in general to accomplish this. – Tim Knight May 22 '12 at 13:37
  • And thank you apneadiving for the direction on where to looking with this issue. – Tim Knight May 22 '12 at 13:49
  • I guess you should simply set the zoom within the helper if you have only one entry in your db. (and you're welcome :) ) – apneadiving May 22 '12 at 14:07
  • Setting zoom required that I use center_latitude and center_longitude as well and then the pan worked great. If you want to submit and actual answer I'd be happy to make it as accepted :). – Tim Knight May 22 '12 at 14:54

3 Answers3

1

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

Due to google maps async loading.

apneadiving
  • 114,565
  • 26
  • 219
  • 213
0

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 )

pjmorse
  • 9,204
  • 9
  • 54
  • 124
Andres Calle
  • 257
  • 1
  • 3
  • 11
0

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