【问题标题】:gmaps4rails change map idgmaps4rails 更改地图 ID
【发布时间】:2014-12-30 06:38:05
【问题描述】:

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

在初始页面加载时,我会在地图上的适当位置显示设施的标记。然而,片刻之后,标记将完全消失。我知道为什么会这样,但不知道如何解决。

我有以下Coffeescript 查找#mapID 并调用/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,标记消失了。我不想失去这个咖啡脚本功能,因为它会正确更新我的 gps 显示视图和标记。我想找出一种方法来生成 gmaps4rails 地图并更改 ID

facility.rb

  acts_as_gmappable process_geocoding: false

facility_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#map,这样我就可以防止标记消失并继续在我的 GPS 视图中使用我的咖啡脚本?

如果有人有任何建议,请告诉我。我将不胜感激。

如果我的问题令人困惑或需要更多解释,我很乐意更新它。

【问题讨论】:

    标签: ruby-on-rails-3 google-maps coffeescript gmaps4rails


    【解决方案1】:

    我想我知道如何在gmaps4rails 中覆盖#mapID

    通过将:map_options 设置为facility_mapID,我的coffeesript 不会在此视图上触发,并且标记保持不变。

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

    如果你们有更好的方法,请告诉我。但到目前为止,这似乎对我有用。

    【讨论】: