2014-11-03 80 views
0

我有一个Rails 3.2.x应用程序,我正在使用gmaps4rails 2.0.0.pre(是的,我知道它是旧版本)。我正在使用它在GPS视图/控制器中绘制地图上的不同车辆(单位)。现在我试图利用gmaps4rails在不同的视图/控制器中显示地图,以在设施显示视图中显示建筑物(设施)位置。gmaps4rails更改地图ID

在初始页面加载时,我在正确的位置在地图上显示设施的标记。但过了一会儿,标记就会完全消失。我知道为什么会发生这种情况,但不知道如何解决。

我有以下Coffeescript它看起来为#map一个ID并调用/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地图默认ID#map。我试图弄清楚如何使用不同的ID插件创建一个新地图,以便我可以显示设施位置,而不用调用咖啡文字刷新。

这是我的设施模型/视图/控制器的样子。一切正常,但由于咖啡脚本调用#map,标记消失。我不想失去这个coffeescript功能,因为它正确地用标记更新我的gps show view。我想找出一种方法来生成gmaps4rails地图并更改ID

facility.rb

acts_as_gmappable process_geocoding: false 

facilities_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,以便我可以防止标记消失并继续在我的GPS视图中使用我的咖啡标记?

如果有人有任何建议,请让我知道。我将不胜感激。

如果我的问题很混乱或需要更多解释,我会很乐意更新它。

回答

1

我想我想通过gmaps4rails覆盖ID#map

通过将:map_options设置为IDfacility_map我的咖啡does不会在此视图上触发,并且该标记保持静态,因为我想要。

<div class="well"> 
    <%= gmaps(markers: {data: @marker, options: {rich_marker: true}}, :map_options => {:id => "facility_map"}) %> 
</div> 

如果您有任何人有更好的方法来做到这一点,请让我知道。但到目前为止,这似乎为我工作。