2016-07-04 4 views
2

저는 레일을 처음 접했고이 마커를 얼마나 정확하게 루프시킬 수 있는지 궁금합니다. My JS 변수 "count"가 인식되지 않아 루비 배열을 반복하거나 다른 해결책이 필요합니다.루비가 내장 된 Google지도 마커를 루핑 하시겠습니까?

1.Add MarkersController에서 작업 :

def index 
     respond_to do |format| 
     format.json do 
      markers = Marker.all.map do |marker| 
      { 
       lat: marker.lat, 
       lng: marker.lng 
      } 
      end 
      render json: markers 
     end 
     end 
    end 

2.In routes.rb

get "/markers", to: "markers#index" 

function initMap() { 
      var mapDiv = document.getElementById('map'); 
      var map = new google.maps.Map(mapDiv, { 
       center: {lat: 44.540, lng: -78.546}, 
       zoom: 8 
      }); 

      var total = <%= mapcount %> 

      var javascriptcount = 0; 

      var count = 0; 

     <% arraylat = [] %> 
     <% arraylng = [] %> 

     <% mapposttotal.each do |q| %> 
      <% arraylat << q.lat %> 
      <% arraylng << q.lng %> 
     <% end %> 

      for (; javascriptcount <= total; javascriptcount++) { 
      var marker = new google.maps.Marker({ 
       position: {lat: <%= arraylat[count] %>, lng: <%= arraylng[count] %>}, 
       map: map, 
       title: 'Hello World!' 
      }); 
      count = count + 1; 
      console.log() 
      } 

     var Clicker = document.getElementById('PostIt'); 


     Clicker.addEventListener('click', function() { 
      window.location='/newpost.html';}, false); 

     } 


    <% end %> 
+0

erb 코드와 자바 스크립트를 섞어 꽤 나쁜 것입니다. ajax 호출을 통해 마커 정보를 얻는 것이 좋습니다 (db에 저장되어있는 경우). 그리고 'success' 응답에 채워주는 것이 좋습니다. – kasperite

답변

1

당신이 레일에 처음으로,이 솔루션을 제안 할 수 있습니다 3. 자바 스크립트 :

function initMap() { 
    $.getJSON("/markers", function(data) { 
    // All your js code to populate markers go in here. 
    }) 
} 

기본적으로 어떻게 작동해야합니다. 그냥 귀하의 필요에 맞게 코드를 맞춤

관련 문제