Leave a comment

Use Draggable marker in gmaps4rails and find current coordinates.

In Rails gmaps4rails it is possible to drag the Marker and get the current co-ordinate by following configuration :

<%= gmaps(“markers” => {“data” => @json, “options” => {“draggable” => true}}, “map_options” => { “type” => “HYBRID”, “center_on_user” => true, “processing” => “json”, “auto_zoom” => false, “zoom” => 30, “auto_adjust” => true, “detect_location” => true}) %>

 

and write following script in footer of this page.

Gmaps.map.HandleDragend = function(pos) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
alert(“You current co-ordinate is ” + pos);
} 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(Gmaps.map.markers[i].serviceObject, ‘dragend’, function() { Gmaps.map.HandleDragend(this.getPosition()) });
}
};

 

and don’t forget to write draggable => true  in markers options as shown in above syntax.

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: