gmaps4rails - drop marker to new position
我有一个应用程序,其中 gps 信息是从带有 gps 数据的照片中提取的。当我想通过编辑操作更新位置时,我想拖动一个标记,替换它并保存新位置。当我点击地图时,旧标记仍然存在,所以有两个标记。一是旧的,二是新的,可拖动。我在 javascript 中更新表单 ID,但未保存新位置。
当我在javascript中检查数组的长度时,那里什么都没有。但是如果我看到页面的来源,数组中的标记很少。
我错过了什么吗?
谢谢
properites_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 | def edit @property = Property.find(params[:id]) @json = Property.find(params[:id]).to_gmaps4rails end def update @property = Property.find(params[:id]) if @property.update_attributes(params[:property]) redirect_to @property, notice: 'Property was successfully updated.' else render action:"edit" end end |
edit.html.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <%= render 'form' %> <%= gmaps("map_options" => {"zoom" => 15,"auto_adjust" => true,"auto_zoom" => false}, "markers" => {"data" => @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.serviceObject, 'click', function(event) { clearOverlays(); placeMarker(event.latLng); updateFormLocation(event.latLng); }); }; // Update form attributes with given coordinates function updateFormLocation(latLng) { $('#property_latitude').val(latLng.lat()); $('#property_longitude').val(latLng.lng()); $('#property_gmaps_zoom').val(Gmaps.map.serviceObject.getZoom()); } // Add a marker with an open infowindow function placeMarker(latLng) { var marker = new google.maps.Marker({ position: latLng, map: Gmaps.map.serviceObject, draggable: true }); markersArray.push(marker); // Set and open infowindow var infowindow = new google.maps.InfoWindow({ content: 'Awesome!<p>Drag me and adjust the zoom level.</p>' }); infowindow.open(Gmaps.map.serviceObject,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].serviceObject.setMap(null); //markersArray[i].setMap(null); } } markersArray.length = 0; } <% end %> |
_form.html.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%= simple_form_for @property, :html => {:multipart => true} do |f| %> <%= f.error_notification %> <%= f.input :name, :label =>"Název", :input_html => { :class =>"input-xlarge"} %> <%= f.input :description, :label =>"Popis", :input_html => { :class =>"input-xlarge", :rows =>"6"} %><br /> <%= f.hidden_field :latitude %> <%= f.hidden_field :longitude %> <%= f.input :date, :label =>"Datum", :as => :date %> <%= f.file_field :photo %> <%= link_to 'Zpět', properties_path, :class => 'btn' %> <%= f.button :submit, :class => 'btn-primary' %> <% end %> |
您使用的
你应该简单地使用