我有一层点和一层线。该层由jbuilder创建,传单显示它。我可以单独创建和显示这些层,但不能放在一起。对于一个层,map.js在一定程度上做到了这一点
var featureLayer = L.mapbox.featureLayer()
.loadURL('map/map_data.geojson')
.addTo(map);
featureLayer.on('ready', function(e) {
map.fitBounds(featureLayer.getBounds());
});map_controller.rb
def map_data
start = '1895-01-01'
start = start.to_date
finish = (start + 3.year)
@lines = RestoResidLine.where(resto_date: start..finish).select("id, person_id, resto_loc_id, resid_loc_id, resto_name, resto_date, title_resto, resid_date, title_resid, long_resto, lat_resto, long_resid, lat_resid")
endmap_data.json.jbuilder
json.type "FeatureCollection"
json.features @lines do |line|
if (line.long_resto && line.long_resid)
json.type "Feature"
json.geometry do
json.type "LineString"
json.coordinates [[line.long_resto, line.lat_resto], [line.long_resid, line.lat_resid]]
end
end
end map_data.json可供map.js使用。但我不明白Rails的魔力,这一切是如何结合在一起的。
我要第二层加点。我首先尝试在控制器中添加第二个json.features @points do |line|...end调用@lines。但只有最后一个出现了。我想他们可能会连在一起。我也许可以改变@.的定义。引入所有所需的信息,这将是一个解决方案;但逻辑将是丑陋的。
所以我尝试创建另一个文件point_data.json.jbuilder并添加到map.js中。
var featureLayer = L.mapbox.featureLayer()
.loadURL('map/point_data.geojson')
.addTo(map);当然,足够多的point_data.json可以生成,但它是一个2400行的html <title>Action Controller: Exception caught</title>。map_data.json已生成并正确显示。
这是命名问题吗?或?我对Rails、ActiveRecord和JavaScript传单相当陌生。
代码:https://bitbucket.org/MtnBiker/crores5/,我会尝试上传这个网站,这样你就可以在操作中看到它。在Heroku上没有正确加载。
这不会那么难的。谢谢。
发布于 2016-10-15 04:28:01
我没有设置找到point_data的路线,显然jbuilder需要这样做。
get 'map' => 'map#index'
get 'map/map_data', :defaults => { :format => 'json' }
get 'map/point_data', :defaults => { :format => 'json' }
get 'map/line_data', :defaults => { :format => 'json' }前两个已经存在,但我忘了添加后两个。我现在用的是后两者。第一个是原作,我把它分成两部分。
当地图在Heroku和localhost的工作方式不同时,帮助了我们挖掘。
忘了这件事。
https://stackoverflow.com/questions/40053889
复制相似问题