2012-02-24 52 views
2

我想实现这一点,从宝石维基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 
    ... 

我缺少的东西?谢谢!

回答

3

从外观上看,该语法是为原型而编写的。如果您在jQuery中使用rails 3.1,则需要更新该语法以查找您的DOM节点。

也就是说,如果你正在寻找与ID "location_attributes_latitude"的元素,你需要使用:

$('#location_attributes_latitude') 

而且为了设定值:

$('#location_attributes_latitude').val(latLng.lat()); 
+0

听起来像你发现问题 – apneadiving 2012-02-24 18:57:14

+0

为了工作,我还必须改变.value = latLng.lat(); to .val(latLng.lat()); – miligraf 2012-02-24 19:46:53

+0

嘿家伙我也卡在这个。我有一个名为“thing”的模型,并使用nested_form来创建一个新的事物实例(:name,:description,:latitude,:longitude)。我想我需要改变功能“updateFormLocation” - 但我不知道如何。它应该是$('#thing_attributes_latitude')。val(latLng.lat())? – adamteale 2012-07-20 15:18:17

0

如果你只需要删除更新存在的标记,只需要做到这一点

 
location_gmaps =() -> 
    Gmaps.map.callback =() -> 
    google.maps.event.addListener Gmaps.map.markers[0].serviceObject, 'dragend', (event) -> 
     updateFormLocationEventos(event.latLng) 

# Update form attributes with given coordinates 
updateFormLocationEventos = (point) -> 
    $('#event_position_latitude').val(point.lat()) 
    $('#event_position_longuitude').val(point.lng()) 

我希望这可以帮助你