Search code examples
ruby-on-railsrubyruby-on-rails-3google-mapsgmaps4rails

add callback to map with Google-Maps-for-Rails


I found stackoverflow topic on adding marker add marker with Google-Maps-for-Rails , but failed even in adding callback :(

In https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Javascript-goodies , found that there were some changes in addressing to the map, so I tried this

<script type="text/javascript" charset="utf-8">
 Gmaps.map.callback = function ()
 {
   google.maps.event.addListener(Gmaps.map, 'click',
           function(object){alert("it works");}
   );
 }
</script>

but again with no luck.

Please, show where I'm wrong.

In the end, I just want to let the user to point some spot on the map and autopopulate fields with latitude and longitude, try to reverse geocode the address for creating yet another place in db.


Solution

  • I guess the problem comes from Gmaps.map.

    Indeed: Gmaps.map is the default namespace for the whole map stuff (functions, markers, map object, polylines...).

    But, the map object itself is Gmaps.map.map.

    I did this to keep things short but it appears to be pretty confusing and now I am here, I prefer not to change: it could confuse people even more. BTW, it's described on the first line here.


    Reminder:

    Additional javascript should be put after the gmaps helper AND in a content_for :scripts

    Ex:

    <%= gmaps(whatever args) %>
    
    <% content_for :scripts do %>
       <script type="text/javascript" charset="utf-8">
         //your js here
       </script>
    <% end %>
    

    If you wonder, why, simply keep in mind that js must exist before being used and it's loaded thanks to the helper (for Rails 3.0.x at least).