Redid places/show using the all-javascript method.

Moved JSON for "what's here?" from members/index to places/index -- we
can improve this later if we want to show things other than members.
This commit is contained in:
Skud
2013-09-11 12:14:18 +10:00
parent f201f357da
commit 3cd48f751e
4 changed files with 42 additions and 37 deletions

View File

@@ -1,21 +1,42 @@
map = L.map('map').setView([0.0, -0.0], 2);
L.tileLayer("http://{s}.tile.cloudmade.com/<%= ENV['CLOUDMADE_KEY'] %>/997/256/{z}/{x}/{y}.png", {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors under <a href="http://www.openstreetmap.org/copyright">ODbL</a> | Imagery &copy; <a href="http://cloudmade.com">Cloudmade</a>',
maxZoom: 18
}).addTo(map);
markers = new L.MarkerClusterGroup();
places_base_path = "/places"
things_to_map = location.pathname + '.json'
$.getJSON('/members.json', function(members) {
$.each(members, function(i, m) {
if (m.latitude && m.longitude) {
marker = new L.Marker(new L.LatLng(m.latitude, m.longitude));
link = "<p><a href='/members/" + m.login_name + "'>" + m.login_name + "</a></p>";
where = "<p><i>" + m.location + "</i></p>";
marker.bindPopup(link + where).openPopup();
markers.addLayer(marker);
}
if (location.pathname == places_base_path) { //places index page
map = L.map('map').setView([0.0, -0.0], 2);
showMap(map);
} else { // specific place page
place = location.pathname.replace(places_base_path + "/", '');
nominatim_query_url = 'http://nominatim.openstreetmap.org/search/' + place;
nominatim_options = {
format: "json",
callback: "placeholder",
limit: 1,
email: "<%= Growstuff::Application.config.user_agent_email %>"
};
$.getJSON(nominatim_query_url, nominatim_options, function(data) {
map = L.map('map').setView([data[0].lat, data[0].lon], 5);
showMap(map);
})
}
function showMap(map) {
L.tileLayer("http://{s}.tile.cloudmade.com/<%= ENV['CLOUDMADE_KEY'] %>/997/256/{z}/{x}/{y}.png", {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors under <a href="http://www.openstreetmap.org/copyright">ODbL</a> | Imagery &copy; <a href="http://cloudmade.com">Cloudmade</a>',
maxZoom: 18
}).addTo(map);
markers = new L.MarkerClusterGroup();
$.getJSON(things_to_map, function(members) {
$.each(members, function(i, m) {
if (m.latitude && m.longitude) {
marker = new L.Marker(new L.LatLng(m.latitude, m.longitude));
link = "<p><a href='/members/" + m.login_name + "'>" + m.login_name + "</a></p>";
where = "<p><i>" + m.location + "</i></p>";
marker.bindPopup(link + where).openPopup();
markers.addLayer(marker);
}
});
});
});
map.addLayer(markers);
map.addLayer(markers);
}

View File

@@ -10,7 +10,6 @@ class MembersController < ApplicationController
respond_to do |format|
format.html # index.html.haml
format.json { render :json => Member.all.to_json(:only => [:id, :login_name, :location, :latitude, :longitude]) }
end
end

View File

@@ -4,6 +4,8 @@ class PlacesController < ApplicationController
def index
respond_to do |format|
format.html
# json response is whatever we want to map here
format.json { render :json => Member.all.to_json(:only => [:id, :login_name, :location, :latitude, :longitude]) }
end
end
@@ -47,6 +49,7 @@ class PlacesController < ApplicationController
respond_to do |format|
format.html # show.html.haml
format.json { render :json => @nearby_members.to_json(:only => [:id, :login_name, :location, :latitude, :longitude]) }
end
end

View File

@@ -1,25 +1,7 @@
-content_for :title, @place
<script src="/assets/leaflet.js?body=1" type="text/javascript"></script>
%div#map{ :style => "height:300px"}
- attribution = render(:partial => 'map_attribution').gsub(/\n/, " ")
:javascript
map = L.map('map').setView([#{ @latitude }, #{ @longitude }]).fitBounds([ [#{@sw_lat}, #{@sw_lng}], [#{@ne_lat}, #{@ne_lng}] ]);
L.tileLayer("http://{s}.tile.cloudmade.com/#{ ENV['CLOUDMADE_KEY'] }/997/256/{z}/{x}/{y}.png", {
attribution: '#{ attribution }',
maxZoom: 18
}).addTo(map);
- Member.located.each do |m|
- popup = render :partial => 'members/popover', :locals => { :member => m }
- popup = escape_javascript(popup).gsub(/\\\//, "/")
:javascript
marker = new L.Marker(new L.LatLng(#{m.latitude}, #{m.longitude}));
marker.bindPopup('<p>#{link_to m, m}</p> #{ popup }').openPopup();
map.addLayer(marker);
%h3
= Growstuff::Application.config.site_name
members near #{@place}