我有一个Rails 3.2.x应用程序,我正在使用gmaps4rails 2.0.0.pre (是的,我知道它是一个旧版本)。我用这个来在gps视图/控制器的地图上绘制不同的车辆(单位)。现在,我试图利用gmaps4rails在不同的视图/控制器中显示地图,以便在设施显示视图中显示建筑物(设施)位置。
在初始页面加载时,我会将设备的标记显示在适当位置的地图上。然而,过了一会儿,标记就会完全消失。我知道为什么会发生这种事,但不知道如何解决。
下面的Coffeescript查找ID of #map,并调用/units和/gps路径,以准实时方式更新GPS视图/地图。
gps.js.coffee
$ ->
if $("#map").length > 0
setInterval (->
$.getScript "/units"
# Get all current locations, find each marker in the map, and update it's position
$.getJSON "/gps", (data) ->
$.each(data, (i, val) ->
marker = $.grep Gmaps.map.markers, (e) ->
e.id is val.id
marker[0].setPosition(val.lat, val.lng) if marker[0]?
)
), 5000
$('.marker-link').on 'click', ->
id = $(this).data("marker-id")
marker = $.grep Gmaps.map.markers, (e) ->
e.id is id
marker[0].showInfowindow() if marker[0]?所以问题是,gmaps4rails映射的默认gmaps4rails是#map。我正在努力弄清楚如何使用带有不同ID的插件创建一个新的映射,这样我就可以显示一个设施位置,而不必调用coffeescript来刷新。
这是我的设施模型/视图/控制器的样子。一切正常,但是由于调用#map的咖啡脚本,标记消失了。我不想失去这个coffeescript功能,因为它正确地更新了我的gps显示视图。我想找出一种生成gmaps4rails映射和更改ID的方法。
facility.rb
acts_as_gmappable process_geocoding: falsefacilities_controller
def show
@facility = Facility.find(params[:id])
@marker = @facility.to_gmaps4rails do |facility, marker|
#marker.infowindow render_to_string(partial: "unit_infowindow", locals: {unit: unit})
marker.json({id: facility.id})
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @marker }
end
end设施show.html.erb
<div class="well">
<%= gmaps(markers: {data: @marker, options: {rich_marker: true, "auto_zoom" => false}}) %>
</div>有没有一种方法可以生成一个gmaps4rails映射,并覆盖默认的ID of #map,这样我就可以防止标记消失,并在我的全球定位系统视图中继续使用我的coffeescript?
如果有人有任何建议,请告诉我。我会非常感激的。
如果我的问题令人困惑或需要更多的解释,我很乐意更新它。
发布于 2014-11-03 15:35:41
我想我想出了如何覆盖ID of #map in gmaps4rails。
通过将:map_options设置为一个ID of facility_map,我的咖啡杯不会在这个视图上开火,标记就像我想要的那样保持静态。
<div class="well">
<%= gmaps(markers: {data: @marker, options: {rich_marker: true}}, :map_options => {:id => "facility_map"}) %>
</div>如果你们有更好的方法做这件事,请告诉我。但到目前为止这似乎对我有用。
https://stackoverflow.com/questions/26717074
复制相似问题