3

I am trying to follow the answer here to zoom out a bit (by default) when a single marker is displayed on the map. I have tried the code below, and have a working map generating, but changing setZoom has no effect. Also, I receive the following error from firebug (below code).

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

<% content_for :scripts do %>
    <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(5);
        }
        else{
         //more than one marker, let's auto_zoom
         Gmaps4Rails.map_options.auto_zoom = true;
         Gmaps4Rails.adjust_map_to_bounds();
        }
      }
    </script>
<% end %>

The only error says:

TypeError: Yc is not a function
[Break On This Error]   

...=b}Rf[F].Fa=xk(7,Yc("f"));me[F].cb=xk(3,function(a){var b;if(b=a.ca[ec]()?j:a.ca...
Community
  • 1
  • 1
Abram
  • 39,950
  • 26
  • 134
  • 184

1 Answers1

6

Ok, the interface changed a lot since I posted the answer you quote.

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

<% content_for :scripts do %>
    <script type="text/javascript" charset="utf-8">
      Gmaps.map.callback = function() {
        if (Gmaps.map.markers.length == 1) {
         //only one marker, choose the zoom level you expect
         setTimeout(function() { Gmaps.map.serviceObject.setZoom(5);}, 50);
        }
        else{
         //more than one marker, let's auto_zoom
         Gmaps.map.map_options.auto_zoom = true;
         Gmaps.map.adjustMapToBounds();
        }
      }
    </script>
<% end %>
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • Thanks for this, and for the gem. Only problem is when I try the above code I get TypeError: Gmaps.map.adjust_map_to_bounds is not a function [Break On This Error] Gmaps.map.adjust_map_to_bounds(); find_map (line 289) TypeError: Yc is not a function [Break On This Error] ...=b}Rf[F].Fa=xk(7,Yc("f"));me[F].cb=xk(3,function(a){var b;if(b=a.ca[ec]()?j:a.ca... – Abram Oct 01 '12 at 07:15
  • I changed this line Gmaps.map.adjusMapToBounds(); to add the 't' in adjust.. and I was able to run the callback without any errors in the console.. However, still no glory on setZoom ... with only one marker showing I wasn't able to modify setZoom.. Always set to max zoom, no matter what :( – Abram Oct 01 '12 at 08:31
  • For now I have just decided to add an extra marker into every json variable with json = json[0..-2] << ",{\"description\":\"Your Location:
    #{params[:location][:address]}\",\"title\":\"Your Location\", \"width\":37, \"picture\":\"http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png\", \"shadow_picture\":\"http://www.google.com/mapfiles/shadow50.png\", \"shadow_anchor\":[12, 35], \"shadow_width\":100, \"shadow_height\":100, \"id\":\"\",\"lat\":#{location.latitude},\"lng\":#{location.longitude}}]"
    – Abram Oct 01 '12 at 08:39
  • mmm, and without your additional marker, what do you have when you add `console.log(Gmaps.map.markers.length)` to the callback? – apneadiving Oct 01 '12 at 15:16
  • So you confirm `Gmaps.map.serviceObject.setZoom(5);` is executed? – apneadiving Oct 02 '12 at 20:55
  • Well, if I put alert(); just below Gmaps.map.serviceObject.setZoom(5); then I see the alert.. so the if statement is running properly, but Gmaps.map.serviceObject.setZoom(5) seems to have no effect. How can I test that the command is executed? – Abram Oct 03 '12 at 07:24
  • By the way, I dumped my Gmaps.map.serviceObject after setZoom and saw xh A: V C: rh __e3_: Object ac: Nf b: HTMLDivElement bc: Gf center: O controls: Array[14] disableDefaultUI: false disableDoubleClickZoom: false draggable: true e: Gf features: V gm_accessors_: Object gm_bindings_: Object mapTypeControl: undefined mapTypeId: "roadmap" mapTypes: Tf maxZoom: null minZoom: null n: Uf overlayMapTypes: Nf re: Gf se: Gf streetView: rh tilt: 0 ve: Gf zoom: 21 __proto__: c – Abram Oct 03 '12 at 07:51
  • google maps is busy processing the bounds and literally ignores the setZoom, the solution is to use a mere timeout, see edit – apneadiving Oct 03 '12 at 11:15
  • You are one of the best devs out there! Thanks for the dedicated support. You the man! – Abram Oct 03 '12 at 19:19
  • Quick question if I may. Using this exact code, I get nada. console shows `Gmaps.map.markers.length = 0`, but there is clearly a single marker created. – Dudo Mar 24 '13 at 06:11
  • @apneadiving, it happens sometimes that 50 ms timeout isn't enough. – Sergey Alekseev Aug 06 '13 at 18:32
  • @SergeyAlekseev yep, I ended up using an event on the map directly, observing idle state – apneadiving Aug 06 '13 at 22:47
  • It's been another 2 years since this answer, is there a more up to date syntax using `handler`? The wiki could use some examples and a page clean up. – Archonic Sep 22 '14 at 20:52
  • @Archonic the wiki's homepage clearly link old documentation as old documentation (Gmaps4rails before v2). Reading the wiki, you may have noticed there are plenty of examples: http://apneadiving.github.io/ – apneadiving Sep 22 '14 at 20:55
  • Awesome! I was using google and stumbled on the old docs not realizing it. Thanks! – Archonic Sep 22 '14 at 21:19