Skip to content

Instantly share code, notes, and snippets.

@mureithi254
Forked from lizvdk/map.js
Created November 10, 2017 12:36
Show Gist options
  • Save mureithi254/39a60ae809ba98862be4572db3371223 to your computer and use it in GitHub Desktop.
Save mureithi254/39a60ae809ba98862be4572db3371223 to your computer and use it in GitHub Desktop.
Creating GeoJson
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 });
});
});
}
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
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