【发布时间】:2014-10-31 01:07:32
【问题描述】:
我的数据库中有管道模型,它具有几何属性(LineString)。我将此添加到管道控制器.rb:
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
这是管道.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!');
}
})
}
})
但是我的管道没有出现在地图上。我究竟做错了什么?也许我的 pipe.json 格式不正确?还是风格不行?
【问题讨论】:
-
您的浏览器控制台是否出现任何错误?你试过调试吗?
-
是的,我在控制器中搞砸了一些东西......
标签: ruby-on-rails geocoding leaflet