Skip to content

Instantly share code, notes, and snippets.

@lizvdk
Last active November 10, 2017 12:42
Show Gist options
  • Save lizvdk/2837a95ae632ed4dad3c to your computer and use it in GitHub Desktop.
Save lizvdk/2837a95ae632ed4dad3c 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
@mureithi254
Copy link

mureithi254 commented Nov 10, 2017

Hello Liz;
If you wanted to update that data to the map say using ajax how would you do it,especially the url part where it would be fetching the data to display to the map;eg

$.ajax({
   type: 'json',
   url: 'what url would you put here?',
 ...
});

I have a project i am working on using maps and i am kinda stuck,its on a private repository on bitbucket but i can pull the bit of code for you to help.Thanks in advance,most appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment