Search code examples
ruby-on-railsgmaps4rails

Accessing the Google map object via Gmaps4Rails API


I am successfully using GMaps4Rails to render markers from a Rails/Mongoid/Spacial model, and I am generally finding GMaps4Rails just unbelievably cool.

However, one thing keeps nagging in the back of my mind (although I got it to work OK): in all examples and answers I've seen the Google map object accessed through Gmaps4Rails.map, e.g.

google.maps.event.addListener(Gmaps4Rails.map, 'click', function(object){ 
  alert(object.latLng);
});

(Taken from add marker with Google-Maps-for-Rails)

However, I never got this to work. If I log the Gmaps4Rails.map object to the console, it is undefined. The Gmaps.map object is of type Gmaps4RailsGoogle, so to get to the actual Google map, I need to write e.g.

google.maps.event.addListener(Gmaps.map.map, 'click', ...)

I am instantiating the map via

 = gmaps(:map_options => {"detect_location" => true, "center_on_user" => true, 
   "zoom" => 12})

Any hints would be much appreciated, as I have a nagging feeling that I am doing something incorrectly.

Update: I should have read https://github.com/apneadiving/Google-Maps-for-Rails/wiki/From-0.x.x-to-1.x.x


Solution

  • I can't see any question here :)

    Since my 1.0.0 release, some things have changed, so yes you're finding dated indications. But you've found the answer using Gmaps.map.map.

    Here:

    • Gmaps is a namespace containing all the maps you could create ion your page

    • all maps containers are found by id and default id is map (I like short expressions)

    • to get the map object from the map container, you append another .map (this is a bit annoying but I kept this to avoid users to become mad)

    But all details for migration from 0.x.x to 1.x.x can be found here (there are few).