-
-
Save mureithi254/39a60ae809ba98862be4572db3371223 to your computer and use it in GitHub Desktop.
Creating GeoJson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function drawIndexReportMap(){ | |
L.mapbox.accessToken = "YOUR_TOKEN"; | |
var map = L.mapbox.map("map", "lizvdk.knp8dn4m", { zoomControl: false }): | |
var featureLayer = L.mapbox.featureLayer().addTo(map); | |
featureLayer.on('layeradd', function (e) { | |
featureLayer.eachLayer(function(layer) { | |
var marker = e.layer, | |
feature = marker.feature; | |
marker.setIcon(L.divIcon(feature.properties.icon)); | |
}); | |
}); | |
featureLayer.loadURL('/reports.json'); | |
map.featureLayer.on('ready', function(e) { | |
featureLayer.eachLayer(function(layer) { | |
item.onclick = function() { | |
map.setView(layer.getLatLng(), 19); | |
layer.openPopup(); | |
}; | |
var content = | |
'<a target="_blank" class="popup" href="' + | |
layer.feature.properties.url + '">' + | |
'<img class="th radius" src="' + | |
layer.feature.properties.photo + | |
'" />' + | |
layer.feature.properties.category + | |
'</a>'; | |
layer.bindPopup(content, { minWidth: 300 }); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Report < ActiveRecord::Base | |
def self.geojson | |
geojson = Hash.new | |
features = [] | |
geojson[:type] = "FeatureCollection" | |
geojson[:features] = features | |
by_recency.each do |report| | |
features << { | |
type: "Feature", | |
geometry: { | |
type: "Point", | |
coordinates: [report.longitude, report.latitude] | |
}, | |
properties: { | |
category: report.category.name, | |
url: "/reports/#{report.id}", | |
photo: report.photo.url, | |
updated_at: report.display_date, | |
id: "report-#{report.id}", | |
icon: { | |
html: report.category.icon, | |
iconSize: [50, 50], | |
iconAnchor: [25, 25], | |
popupAnchor: [0, -25], | |
className: "#{report.marker_color} map-icon" | |
} | |
} | |
} | |
end | |
geojson | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReportsController < ApplicationController | |
def index | |
@reports = Report.by_recency | |
@geojson = Report.geojson | |
respond_to do |format| | |
format.html | |
format.json { render json: @geojson } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment