English 中文(简体)
利用 j和铁路显示谷歌地图的数据
原标题:Using jQuery and Rails to display data on a Google Map

我有麻烦地从我的铁路机上获得JSON数据,以在谷歌地图上使用 j。 在我的控制员中:

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

然后,在我的地图上,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(); } );

我先从上改编,该地图显示的是罚款,但没有任何标识(手上加)。 I m 采用铁路3 beta 3和Corr.1。 给JSON的产量似乎很微,我可以检索到E/places.json的数据。 任何帮助都会受到高度赞赏——事先感谢!

最佳回答

Solved! 我确实是真的,但鲁比拉将JSON作为简单的物体,因此 j。 位置是毫无意义的——json.length是完美的。 新的法典是:

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

所有标识都像预期的那样出现。

问题回答

你们是否试图改变你们要求“/地点.json”的道路?





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签