I was playing with https://github.com/apneadiving/Google-Maps-for-Rails and i\'d like to clear all the marker on a map and add a new one at the position clicked by the user on t
If you want to clear markers created by gmaps4rails
, use this js function:
Gmaps4Rails.clear_markers();
otherwise, loop through your markers and make marker.setMap(null)
Well, the following function removes all markers and add a new one where user clicks:
var marker = null;
function gmaps4rails_callback() {
Gmaps4Rails.clear_markers();
if (marker != null) { marker.setMap(null); }
google.maps.event.addListener(Gmaps4Rails.map, 'click', function(object){ marker = new google.maps.Marker({position: object.latLng, map: Gmaps4Rails.map});});
}
Notes:
object.latLng.lat()
, longitude with: object.latLng.lng()
Another way to add markers is to use the add_marker
function described here: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Dynamic-%28or-Ajax%29-map-refresh