2010-06-02 3 views
2

Rails 앱의 JSON 데이터를 jQuery를 사용하여 Google지도에 표시하는 데 문제가 있습니다. 내 map.js 파일에서 다음jQuery와 Rails를 사용하여 Google지도에 데이터 표시

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 

: 그리고 내 컨트롤러에서

$(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에서 내 코드에 적응 한 -지도 잘하지만 현재 내 마커가 하나도 없다 (추가 표시 손). 나는 레일즈 3 베타 3과 루비 1.9.1을 사용하고 있습니다. JSON 출력은 괜찮아 보입니다 .-places.json의 데이터에 액세스 할 수 있습니다. 어떤 도움이라도 대단히 감사 할 것입니다 - 미리 감사드립니다!

답변

1

해결! 나는 깨닫지 못했다.하지만 Ruby는 JSON을 간단한 객체 배열로 출력하므로 json.Places.length는 의미가 없다. json.length는 완벽하게 작동한다. 새 코드는 다음과 같습니다.

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

모든 마커가 예상대로 나타납니다.

1

getJSON 호출의 경로를 "/places.json"으로 변경해 보았습니까?

+0

네,하지만 슬프게도 기쁨이 없습니다! 또한 "/places.js"와 함께 컨트롤러에 format.js {: render => @places}를 시도했습니다 ... – Budgie

관련 문제