I have a layer of points and a layer of lines. The layer is created by jbuilder and Leaflet displays it.
I can create and display the layers individually, but not together.
For one layer, map.js
has this in part
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")
end
map_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 is made available to map.js
. But I don't understand the Rails magic of how all of this comes together.
I want a second layer with points. I first tried just adding a second json.features @points do |line|...end
calling @lines
in the controller. But only the last one shows up. I thought they might concatenate. I can probably change the definition of the @... to bring in all the needed information, so that would be one solution; but the logic would be ugly.
So I tried creating another file point_data.json.jbuilder
and adding to map.js
var featureLayer = L.mapbox.featureLayer()
.loadURL('map/point_data.geojson')
.addTo(map);
Sure enough point_data.json gets build, but it's a 2400-line html <title>Action Controller: Exception caught</title>
. The map_data.json
is built and displays correctly.
Is this a naming problem? Or? I'm rather new to Rails, ActiveRecord and JavaScript Leaflet.
Code: https://bitbucket.org/MtnBiker/crores5/ I'll try to upload the site so you can see it in (in)action. Not loading correctly on Heroku.
This can't be that hard. Thank you.
I hadn't set the routes to find point_data, and apparently jbuilder needed that.
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' }
First two already existed, but I forgot to add the latter two. I am now using the latter two. The first was the original, and I split it into these two.
Part of what helped was digging around when the map worked differently on Heroku than at localhost
Forgot about this piece.