Last active
November 10, 2017 12:42
-
-
Save lizvdk/2837a95ae632ed4dad3c 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
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
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.