2014-10-31 8 views
0

У меня есть модель трубы в моей базе данных и она имеет атрибут геометрии (LineString). Я добавил это pipes_controller.rb:Как получить LineString для показа с помощью Листовки?

def index 
    @pipes = Pipe.all 
    @geojson = Array.new 

    @pipes.each do |pipe| 

    @geojson<< { 
     type: "FeatureCollection", 
     crs: { type: "name", properties: { name: "urn:ogc:def:crs:OGC:1.3:CRS84" } }, 
     features: [ 
     type: 'Feature', 
     properties: { 
      geometry: { 
      type: 'LineString', 
      coordinates: pipe.get_coordinates 
      }, 
      stroke: "#1087bf" 
     } 
    ]} 
    end 
    respond_to do |format| 
     format.html 
     format.json { render json: @geojson } # respond with the created JSON object 
    end 
end 

Это pipes.js файл:

$(document).ready(function() { 
    if ($("#pipes_map").length>0) { 
    var geojson; 
    var map = L.map('pipes_map', { 
     center: [42.44, 19.26], 
     zoom: 16 
    }); 
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { 
     max_zoom: 22 
    }).addTo(map); 

    L.geoJson(geojson).addTo(map); 
    $.ajax({ 
     dataType: 'text', 
     url: 'http://localhost:3000/pipes.json', 
     success: function(data) { 
     var myStyle = { 
      "color": "#ff7800", 
      "weight": 5, 
      "opacity": 0.65 
     }; 
     geojson = $.parseJSON(data); 
     L.geoJson(geojson).addTo(map);     
     }, 
     error : function() { 
     alert('Error!'); 
     } 
    }) 
    } 
}) 

Но мои трубы не появляются на карте. Что я делаю не так? Может быть, мои трубы.json плохо сформированы? Или стиль не в порядке?

+0

У вас возникли ошибки в консоли вашего браузера? Вы пробовали отладку? –

+0

Да, я что-то испортил в контроллере ... – ivanacorovic

ответ

0

Это то, что контроллер должен выглядеть следующим образом:

def index 
@pipes = Pipe.all 
@geojson = Array.new 

@pipes.each do |pipe| 

    @geojson<< { 
    type: 'Feature', 
    properties: { 
     :category=> pipe.category.name 
    }, 
    geometry: { 
     type: 'LineString', 
     coordinates: pipe.get_coordinates 
    } 
    } 
end 
respond_to do |format| 
    format.html 
    format.json { render json: @geojson } # respond with the created JSON object 
end 

конца

Остальное прекрасно.

Смежные вопросы