gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:30:32 24 4
gpt4 key购买 nike

我在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com