2014-10-31 28 views
0

我在我的数据库中有管道模型,它有几何属性(LineString)。我说这pipes_controller.rb:如何让LineString使用Leaflet显示?

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!'); 
     } 
    }) 
    } 
}) 

但我的管道不会出现在地图上。我究竟做错了什么?也许我的pipes.json格式不好?或风格不好?

+0

在浏览器的控制台中是否有错误?你尝试过调试吗? – 2014-11-03 11:39:11

+0

是的,我搞乱了一些东西在控制器中... – ivanacorovic 2014-11-03 13:30:23

回答

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 

剩下的就是罚款。

相关问题