我想实现这一点,从宝石维基https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Javascript-goodiesgmaps4rails - 删除标记,并更新字段属性的形式
<% 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').value = latLng.lat();
$('location_attributes_longitude').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 %>
,但没有运气......我猜它更多的问题与我的领域(s)的名称/编号,请原谅我的JavaScript知识。
我已经改变等领域的坐标更新:
function updateFormLocation(latLng) {
$('location[lat]').value = latLng.lat();
...
}
但现场没有更新:
= simple_form_for :location do |l|
= l.input :lat
...
我缺少的东西?谢谢!
听起来像你发现问题 – apneadiving 2012-02-24 18:57:14
为了工作,我还必须改变.value = latLng.lat(); to .val(latLng.lat()); – miligraf 2012-02-24 19:46:53
嘿家伙我也卡在这个。我有一个名为“thing”的模型,并使用nested_form来创建一个新的事物实例(:name,:description,:latitude,:longitude)。我想我需要改变功能“updateFormLocation” - 但我不知道如何。它应该是$('#thing_attributes_latitude')。val(latLng.lat())? – adamteale 2012-07-20 15:18:17