Skip to content
apneadiving edited this page Aug 30, 2011 · 28 revisions

Keep in mind the gem's conventions:

  • prior to v1.0.0, only one map could be created. It's properties were accessible through the Gmaps4Rails namespace

From v1.0.0:

  • One map object is instantiated per map displayed. All these objects are independant.

  • all javascript map objects relative are stored in the Gmaps object. To access these, simply type: Gmaps.the_map_id. By default, it's Gmaps.map

Multiple maps

Before v1.0.0, adding multiple maps on a single page was a pain and adding maps from different providers was impossible.

This has now been fixed even though the initial API slightly changed.

There are few cases to distinguish, relative to the proper js files inclusion.

Default implementation

Same Map API

Displaying a second map on the same page involves natural constraints:

  • passing a different id to the second map

  • telling explicitly that the js files shouldn't be loaded twice

This results in:

<%= gmaps(:markers     => {:data => @json1 },
          :map_options => { :auto_adjust => true }) %>
<%= gmaps(:markers     => @json2 },
          :map_options => { :id => "second_map", :center_on_user => true, :zoom => 5 },
          :scripts     => :none ) %>

Adding map from different map providers:

This involves two constraints:

  • having a different id for the second map

  • loading the necessary js

This results in:

<%= gmaps(:markers     => {:data => @json1 },
          :map_options => { :auto_adjust => true }) %>
<%= gmaps(:markers     => @json2 },
          :map_options => { :id => "second_map", :provider => "openlayers" },
          :scripts     => :api) %>

JS Functions

Gmaps4rails allows you to update your map on the fly.

Check here the valid json format you should provide: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Control-what-I-display

Callback

If you want to create a callback called once the map and items are created, include the following code in your view:

<script type="text/javascript" charset="utf-8">
Gmaps.map.callback = function() {
   //whatever you want here
}
</script>

Markers

If you want to replace the current markers with your new ones:

 Gmaps.map.replaceMarkers(your_markers_json_array); //this is a JS function

If you want to add markers to those already displayed (it doesn't check duplicates):

 Gmaps.map.addMarkers(your_markers_json_array);     //this is a JS function

Others

Notice that you can use all javascript functions written in the js files directly. I hope the files have enough comments to make everything clear.


Marker Dragging

Gmaps4rails allows you to update your map on the fly.

Check here the valid json format you should provide: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Control-what-I-display

Callback

If you want to create a callback called once the map and items are created, include the following code in your view:

<script type="text/javascript" charset="utf-8">
Gmaps.map.callback = function() {
   //whatever you want here
}
</script>

Markers

If you want to replace the current markers with your new ones:

 Gmaps.map.replaceMarkers(your_markers_json_array); //this is a JS function

If you want to add markers to those already displayed (it doesn't check duplicates):

 Gmaps.map.addMarkers(your_markers_json_array);     //this is a JS function

Others

You can generate javascript to update your map the same way it's used to create it. Simply use the to_gmaps4rails method after your hash of variables to display as described here: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Methods

Notice that you can use all javascript functions written in the js files directly. I hope the files have enough comments to make everything clear.