jquery - 使用 jQuery 和 Rails 在 Google map 上显示数据

标签 jquery ruby-on-rails google-maps

我在使用 jQuery 从 Rails 应用程序获取 JSON 数据以显示在 Google map 上时遇到问题。在我的 Controller 中:

class PlacesController < ApplicationController

  respond_to :html, :xml, :json

  # GET /places
  # GET /places.xml
  # GET /places.json
  def index
    @places = Place.all

    respond_to do |format|
      format.html # index.html.erb 
      format.json { render :json => @places }
      format.xml  { render :xml => @places }
    end
  end

然后在我的 map.js 文件中:

$(document).ready(function(){
    if (GBrowserIsCompatible()) {
    var map = new google.maps.Map2($("#map").get(0));
        var burnsvilleMN = new GLatLng(44.797916,-93.278046);
        map.setCenter(burnsvilleMN, 8);
        map.setUIToDefault();

        $.getJSON("/places", function(json) {
            if (json.Places.length > 0) {
                for (i=0; i<json.Places.length; i++) {
                    var place = json.Places[i];
                    addLocation(place);
                }
            }
        });

        function addLocation(place) {
            var point = new GLatLng(place.lat, place.lng);      
            var marker = new GMarker(point);
            map.addOverlay(marker);
        }
      }
});

$(window).unload( function () { GUnload(); } );

我已经从 this tutorial 改编了我的代码- map 显示正常,但没有我的标记(手动添加)。我正在使用 Rails 3 beta 3 和 Ruby 1.9.1。 JSON 的输出似乎很好 - 我可以访问/places.json 中的数据。任何帮助将不胜感激 - 提前致谢!

最佳答案

您是否尝试将 getJSON 调用中的路径更改为“/places.json”?

关于jquery - 使用 jQuery 和 Rails 在 Google map 上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2962026/

相关文章:

使用鼠标按住 Javascript 可以更快地增加值

javascript - jqgrid图标的onclick下拉列表

javascript - 谷歌地图控件隐藏

ruby-on-rails - ActiveRecord:将 has_many 列表视为一个简单的数组

ruby-on-rails - Arel中的嵌套查询

android - 从一个项目中调用另一个项目中的类?

javascript - 如何在 Google Maps API 中仅对单个标记进行动画处理

javascript - 使用getJSON时,如何抓取响应数据?

javascript - 如何获取重复对象键的长度

ruby-on-rails - paper_trail 显示成员更改日志(没有针对每个错误的方法)