【问题标题】:How do get LineString to show using Leaflet?如何让 LineString 使用 Leaflet 显示?
【发布时间】: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


【解决方案1】:

控制器应该是这样的:

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

结束

其他都还好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    相关资源
    最近更新 更多