【问题标题】:Trying to populate gmaps4rails with multiple json strings in one page尝试在一页中使用多个 json 字符串填充 gmaps4rails
【发布时间】:2011-12-01 16:39:15
【问题描述】:

我希望我问这个问题是正确的,所以请让我知道我是否有路要走。

问题是试图建立一个从多个控制器中提取的主页,以显示来自多个控制器的最近位置,即。食品、企业等。

现在各个列表页面都有各自的地图

@json = Controller.all.to_gmaps4rails

我该怎么做:

@json = Controller1 Controller2 .all.to_gmaps4rails

我希望这不是一个菜鸟问题,我只是度过了糟糕的一天。谢谢大家!

edit 12.5.2011 @seanhill - 这是模型之一,其他部分非常接近这种格式。首先,我什至不确定我的主页是否需要它自己的模型,因为它根本不与数据库交互,更多的是从执行工作的控制器中提取数据。感谢肖恩的回复!

class Dining < ActiveRecord::Base
validates_uniqueness_of :name, :message => "already exists"

attr_accessible :name, :address, :cuisine, :latitude, :longitude, :about, :facebook, :twitter, :phone, :website
geocoded_by :address
after_validation :geocode, :if => :address_changed?

acts_as_gmappable :process_geocoding => false

def gmaps4rails_address
    "#{self.address}"
end

def gmaps4rails_infowindow
    "<h3>#{self.name}</h3><br /><h5>#{self.cuisine}</h5>"
end

def self.search(search)
    if search
        where('name LIKE ?', "%#{search}%")
    else
        scoped
    end
end

end

【问题讨论】:

  • Controller1 Controller2 是什么意思?你不应该在控制器上调用.all。你应该在模型上这样做。您能给我们一些关于您的模型是如何构建的信息吗?
  • 肖恩,很抱歉-我正在寻找来自不同控制器的信息,即。商店和餐馆,在主页上的 1 个地图中。下面的答案有效,但我有兴趣将信息从控制器移动到模型。目前,模型是近乎基本的,gmappable、_address 和 _infowindow 以及内置在模型中的搜索。

标签: ruby ruby-on-rails-3.1 gmaps4rails rails-3.1


【解决方案1】:

试试这个

holder = Controller1.all
holder << Controller2.all
@json = holder.flatten.map{|h| {lng: h.longitude, lat: h.latitude, class: h.class.to_s}}.to_json

确保根据你的列名更改longitudelatitude,并使用js根据类来操作标记。

正如@Sean Hill 所说,您不应该在控制器上调用.all,但我认为您对事情的运作方式有一点误解。假设您有一个名为DiningModel 和另一个名为Shop,当您在class DiningsController &lt; ApplicationController 中调用Dining.allShop.all 时,您将在Dining ModelShop Model 上调用.all不在DiningsController 上。

您通过控制器显示的信息仅受您在其中调用的方法的限制,但最佳做法是确保显示信息的主要焦点与相应的控制器相关。

因此,您真正想做的是从多个模型中获取记录并将它们组合在一起以在单个地图中显示它们。

话虽如此,答案应该是这样的

holder = Dining.all # Takes all Dining records returned as an array and sets them to holder variable 
holder << Shop.all # Pushes the Shop records array into the holder with the dining records
holder.flatten!# Next we flatten the array so we only have a single array. 

# Then we use the map method to run the given code one time for each instance 
# in the holder array to extract the info we need. The results for every instance 
# in holder are returned in an array which we then convert to_json.
@json = holder.map{|h| {lng: h.longitude, lat: h.latitude, class: h.class.to_s}}.to_json  

【讨论】:

  • 老兄。老板!对我来说就像一个魅力。因此,通过创建一个“持有人”并赋予嵌套控制器的属性,调用该“持有人”将显示它下面的所有内容?很高兴知道!谢谢乔伊
  • 我认为这可以显示每个位置,但我需要区分位置,即餐厅与商店。这个模型只允许大量显示所有包含的链接,对吗?
  • 我有显示 lng、lat 和类的 json 提要,但是如何调用类来显示特定标记并链接到该页面的信息?谢谢!
  • 您超出了原始问题的范围。我上面提供的 json 可以让你用 js 在浏览器中识别哪个标记属于哪个类。使用页面上的地图,在 Web 控制台中尝试 Gmaps.map.markers[0] 以查看已识别的类。从那里您可以根据您的需要编写类似于http://stackoverflow.com/questions/6035984/adding-an-event-listener-to-the-markers-using-gmaps4rails 的代码。您还应该查看https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Markers 此处提供的其他方法。我也更新了我的答案。
【解决方案2】:
@json1 = something.to_gmaps4rails
@json2 = something.to_gmaps4rails

@json = (JSON.parse(@json1) + JSON.parse(@json2)).to_json

【讨论】:

    【解决方案3】:

    我用我最初的节日数据填充了地图,然后用这段代码用 javascript 添加了游乐设施,

    <% content_for :scripts do %>
      <script type="text/javascript">
    
        Gmaps.map.callback = function() {
    
          $.getJSON('/rides_gmap', function(data){
              Gmaps.map.addMarkers(data);
            });
    
          }
      </script>
    <%end%>
    

    在游乐设施控制器中我有这个

    def rides_gmap
        @rides = Ride.all
        @json = @rides.to_gmaps4rails do |ride, marker|
          marker.infowindow render_to_string(:partial => "/rides/infowindow", :locals => { :ride => ride})
          marker.picture({
                    'picture' => view_context.image_path("orange-dot.png"),
                    'width'   => 20,
                    'height'  => 20
                   })
          marker.title "#{ride.address}"
          marker.json({:ride_id => ride.id, :ride_festivaltype => ride.festival.festivaltype 
        end
        respond_with @json
      end
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 2014-09-06
      • 2020-06-20
      • 2023-04-10
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多