Created
January 23, 2012 14:35
-
-
Save sowenjub/1663423 to your computer and use it in GitHub Desktop.
Place a new marker and update fields in a form with gmaps4rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= gmaps4rails(@json) %> | |
<% content_for :scripts do %> | |
<script type="text/javascript" charset="utf-8"> | |
var markersArray = []; | |
// On click, clear markers, place a new one, update coordinates in the form | |
Gmaps.map.callback = function() { | |
google.maps.event.addListener(Gmaps.map.map, 'click', function(event) { | |
clearOverlays(); | |
placeMarker(event.latLng); | |
updateFormLocation(event.latLng); | |
}); | |
}; | |
// Update form attributes with given coordinates | |
function updateFormLocation(latLng) { | |
$('location_attributes_latitude_str').value = latLng.lat(); | |
$('location_attributes_longitude_str').value = latLng.lng(); | |
$('location_attributes_gmaps_zoom').value = Gmaps.map.map.getZoom(); | |
} | |
// Add a marker with an open infowindow | |
function placeMarker(latLng) { | |
var marker = new google.maps.Marker({ | |
position: latLng, | |
map: Gmaps.map.map, | |
draggable: true | |
}); | |
markersArray.push(marker); | |
// Set and open infowindow | |
var infowindow = new google.maps.InfoWindow({ | |
content: '<div class="popup"><h2>Awesome!</h2><p>Drag me and adjust the zoom level.</p>' | |
}); | |
infowindow.open(Gmaps.map.map,marker); | |
// Listen to drag & drop | |
google.maps.event.addListener(marker, 'dragend', function() { | |
updateFormLocation(this.getPosition()); | |
}); | |
} | |
// Removes the overlays from the map | |
function clearOverlays() { | |
if (markersArray) { | |
for (var i = 0; i < markersArray.length; i++ ) { | |
markersArray[i].setMap(null); | |
} | |
} | |
markersArray.length = 0; | |
} | |
</script> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment