gmaps4rails-将标记放到新位置
gmaps4rails - drop marker to new position
我有一个应用程序,可以从带有gps数据的照片中提取gps信息。当我想通过编辑操作更新位置时,我想拖动一个标记,替换它并保存新位置。当我点击地图时,旧的标记仍然存在,所以有两个标记。第一个是旧的,第二个是新的,可拖动。我用javascript更新表单id,但新位置没有保存。当我在javascript中检查数组的长度时,那个里什么都并没有。但若我看到页面的来源,数组中几乎并没有标记。我是不是错过了什么?
谢谢
properites_controller.rb
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
<%= render 'form' %>
<div id="map_canvas">
<%= gmaps("map_options" => { "zoom" => 15, "auto_adjust" => true, "auto_zoom" => false},
"markers" => {"data" => @json}) %>
</div>
<% 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: '<div class="popup"><h2>Awesome!</h2><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;
}
</script><% end %>
_form.html.erb
<%= simple_form_for @property, :html => {:multipart => true} do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= 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 %>
</div>
<div class="form-actions">
<%= link_to 'Zpět', properties_path, :class => 'btn' %>
<%= f.button :submit, :class => 'btn-primary' %>
</div>
<% end %>
您使用的markersArray
不是由gem的javascript维护的。
您应该简单地使用Gmaps.map.markers
,它包含所有标记信息。
相关文章:
- gmaps4rails-将标记放到新位置
- 使用 bindTo() 无法删除新位置的标记
- 从可移动标记获取新位置
- 对象以编程方式移动后的 Fabricjs 在其新位置上不可选择
- Mootools拖动完成后,获取元素的新位置
- 谷歌地图点击平移到新位置 递归错误太多
- 有没有办法让我的.js文件自毁,或重命名自己,或者在一次使用后将自己移动到新位置
- 使用 UI 可排序,在单击按钮时根据给定值将项目移动到新位置
- 标记新位置已创建,但旧位置也显示在 ajax 谷歌地图中
- 在 Firebase 中使用用户的副本时,$conf 美元会添加到对象中,因此我无法将用户复制到新位置
- “后退”按钮重写以重定向回新位置
- 如何将HTML表行拖放到另一个位置(在Jsp中)并将新位置(值)也保存在数据库(MySql)中
- fullcalendar中删除的外部元素的结束时间没有'拖动到新位置之前不应用
- 如何打乱Javascript数组,确保每个索引都在新数组中的新位置
- 如何将高图的数据标签更改到新位置
- 是否有将整个SVG路径移动到新位置的命令
- 如何使用scrollTo插件滚动到元素的新位置
- svg没有设置圆的新位置坐标.(exampe)
- 在径向树布局中拖动节点组,单击时不跳转到新位置
- 的位置.Href无法加载新位置