Skip to content

Navigation Menu

Sign up
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 are stored in the Gmaps object. To access these, simply type: Gmaps.the_map_id. By default, it's Gmaps.map


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

Marker dragging is a recurrent question.

I can't see now how to code a clever and generic solution.

For now I suggest the following: adding this behavior thanks to the callback function.

I'd do as follows:

<%= gmaps("markers" => {"data" => @json, "options" => { "draggable" => true } } ) %> 
<% content_for :scripts do %>
<script type="text/javascript" charset="utf-8">
Gmaps.map.HandleDragend = function(pos) {
 var geocoder = new google.maps.Geocoder();
 geocoder.geocode({
 latLng: pos
 }, function(responses) {
 if (responses && responses.length > 0) {
 alert(responses[0].formatted_address);
 } else {
 alert('Cannot determine address at this location.');
 }
 });
};
Gmaps.map.callback = function() {
 for (var i = 0; i < this.markers.length; ++i) {
 google.maps.event.addListener(Gmaps4Rails.markers[i].serviceObject, 'dragend', function() { Gmaps.map.HandleDragend(this.getPosition()) });
 }
}; 
</script>
<% end %>

Any remark is welcome.

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /