-
Notifications
You must be signed in to change notification settings - Fork 382
Javascript goodies
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:
-
when you don't pass any
id
in themap_options
, the map's id will bemap
-
all javascript objects relative to maps are stored in the
Gmaps
object. To access these, simply type:Gmaps.the_map_id
. By default, it'sGmaps.map
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.
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 ) %>
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) %>
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
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>
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
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.
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
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>
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
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.