8

I am adding google maps support with apneadiving / Google-Maps-for-Rails (thanks awesome gem)

I am finding one slight glitch, however, which very likely is my fault.

auto_zoom works great when there are multiple markers. However, when there is only one marker it is zoomed in to the max level which is not pretty.

"zoom" will only work when auto_zoom is false, so that's not what I want.

So therefore you could use "maxZoom" but now users cannot zoom in manually beyond that point which is not what I want.

Is there a way around this? Is my explanation making sense? Is this a limitation of Google Maps API?

Thanks...

apneadiving
  • 114,565
  • 26
  • 219
  • 213
Enric Ribas
  • 629
  • 6
  • 15

3 Answers3

17

This behavior is due to the auto_zoom built-in function in the google maps api.

One work around to this is to set it to false in the gmaps method:

<%= gmaps({
       "map_options" => { "auto_zoom" => false},
       "markers"     => { "data" => @json }
      })
%>

And then use the gmaps4rails_callback to fit your needs (be sure to have at least version 0.7.9)

<script type="text/javascript" charset="utf-8">
  function gmaps4rails_callback() {
    if (Gmaps4Rails.markers.length == 1) {
     //only one marker, choose the zoom level you expect
     Gmaps4Rails.map.setZoom(2);
    }
    else{
     //more than one marker, let's auto_zoom
     Gmaps4Rails.map_options.auto_zoom = true;
     Gmaps4Rails.adjust_map_to_bounds();
    }
  }
</script>
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • where do you put this callback? in the view? if so, where exactly? – pruett Jul 22 '11 at 18:12
  • @pruett: I slightly changed the api since 0.10.0, see an example here: http://stackoverflow.com/questions/6741452/how-to-get-the-id-field-and-send-to-the-view – apneadiving Jul 22 '11 at 18:42
  • @apneadiving: hmm...not sure i understand how to implement this. i'm using 0.10.2 but can't seem to get the zoom out of max zoom. i just have one marker per page and am trying a simple solution like the one below but it doesn't work. any thoughts? – pruett Jul 22 '11 at 19:09
  • @apneadiving: i'm trying this: `<%= gmaps({ "map_options" => {"auto_adjust" => false, "auto_zoom" => false, "zoom" => 15 }, "markers" => {"data" => @json } }) %>` this doesn't seem to be working for me. i'm trying to simply get the zoom off of it's maximum default setting. – pruett Jul 22 '11 at 19:30
  • @pruett: please ask a new question: it could be useful to someone else – apneadiving Jul 22 '11 at 19:31
  • @apneadiving my bad :) i posted it here: [http://stackoverflow.com/questions/6795200/adjust-zoom-gmaps4rails] thanks for your help! – pruett Jul 22 '11 at 19:41
2

I achieved this in a slightly different way as I know that I'll only ever have one marker on my map. I'm relatively new to rails, but this method feels a bit "cleaner" than using JS in your view.

I've got lat and lng stored in my model (encoded by geokit at time of creation), so did the following in my view:

<%= gmaps({
       "map_options" => {"auto_zoom" => false, "zoom" => 15, "center_latitude" => @listing.lat, "center_longitude" => @listing.lng },
       "markers"     => {"data" => @markers }
      })
%>

@markers is my JSON created by blah.to_gmaps4rails, and "listing" is my model.

DaveStephens
  • 1,126
  • 12
  • 16
  • Yes. That works properly. But, my problem came when I added more than one marker. Then you need to do the javascript solution above. Thanks! – Enric Ribas May 02 '11 at 15:19
0

thanks this helped me...

{"auto_zoom" => false, "zoom" => 15, "center_latitude" => @listing.lat, "center_longitude" => @listing.lng }, "markers" => {"data" => @markers } }) %>
jimmi
  • 1