From df55a5b0a8b04847c241535a11aa3fd9382ec875 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 16 Nov 2019 10:37:40 +1300 Subject: [PATCH] Add marker for leaflet maps, and use asset pipeline to serve file --- app/assets/images/spade-marker.svg | 9 +++++ app/assets/javascripts/places.js.erb | 39 ++++++++++++------- app/assets/stylesheets/leaflet_overrides.scss | 12 +++--- app/views/places/index.html.haml | 3 ++ 4 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 app/assets/images/spade-marker.svg diff --git a/app/assets/images/spade-marker.svg b/app/assets/images/spade-marker.svg new file mode 100644 index 000000000..791a3709e --- /dev/null +++ b/app/assets/images/spade-marker.svg @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/assets/javascripts/places.js.erb b/app/assets/javascripts/places.js.erb index f0322b38b..949563252 100644 --- a/app/assets/javascripts/places.js.erb +++ b/app/assets/javascripts/places.js.erb @@ -1,20 +1,23 @@ if (document.getElementById("placesmap") !== null) { - places_base_path = "/places"; - mapbox_map_id = "<%= Rails.env == 'test' ? 0 : Rails.application.config.mapbox_map_id %>"; - mapbox_access_token = "<%= Rails.env == 'test' ? 0 : Rails.application.config.mapbox_access_token %>"; - mapbox_base_url = "https://a.tiles.mapbox.com/v4/" + mapbox_map_id + "/{z}/{x}/{y}.png?access_token=" + mapbox_access_token; - nominatim_base_url = 'https://nominatim.openstreetmap.org/search/'; - nominatim_user_agent_email = "<%= Rails.env == 'test' ? 0 : Rails.application.config.user_agent_email %>"; + var places_base_path = "/places"; + var mapbox_map_id = "<%= Rails.env == 'test' ? 0 : Rails.application.config.mapbox_map_id %>"; + var mapbox_access_token = "<%= Rails.env == 'test' ? 0 : Rails.application.config.mapbox_access_token %>"; + var mapbox_base_url = "https://a.tiles.mapbox.com/v4/" + mapbox_map_id + "/{z}/{x}/{y}.png?access_token=" + mapbox_access_token; + var nominatim_base_url = 'https://nominatim.openstreetmap.org/search/'; + var nominatim_user_agent_email = "<%= Rails.env == 'test' ? 0 : Rails.application.config.user_agent_email %>"; L.Icon.Default.imagePath = '/assets' + var placesmap; + if (location.pathname === places_base_path) { //places index page placesmap = L.map('placesmap').setView([0.0, -0.0], 2); showMap(placesmap); - } else { // specific place page - place = location.pathname.replace(places_base_path + "/", ''); - nominatim_query_url = nominatim_base_url + place; - nominatim_options = { + } + else { // specific place page + var place = location.pathname.replace(places_base_path + "/", ''); + var nominatim_query_url = nominatim_base_url + place; + var nominatim_options = { format: "json", callback: "placeholder", limit: 1, @@ -32,15 +35,21 @@ function showMap(placesmap) { attribution: 'Map data © OpenStreetMap contributors under ODbL | Map imagery © Mapbox', maxZoom: 18 }).addTo(placesmap); - markers = new L.MarkerClusterGroup({showCoverageOnHover: false, maxClusterRadius: 20 }); + var markers = new L.MarkerClusterGroup({showCoverageOnHover: false, maxClusterRadius: 20 }); - things_to_map = location.pathname + '.json'; + var things_to_map = location.pathname + '.json'; + var default_marker_icon = L.icon({ + iconUrl: "<%=image_url('spade-marker.svg')%>", + iconSize: [48, 48], + iconAnchor: [24, 48], + popupAnchor: [0, -46], + }); $.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 = "

" + m.login_name + "

"; - where = "

" + m.location + "

"; + var marker = new L.Marker(new L.LatLng(m.latitude, m.longitude), {icon: default_marker_icon}); + var link = "

" + m.login_name + "

"; + var where = "

" + m.location + "

"; marker.bindPopup(link + where).openPopup(); markers.addLayer(marker); } diff --git a/app/assets/stylesheets/leaflet_overrides.scss b/app/assets/stylesheets/leaflet_overrides.scss index aa69bb297..c89f27303 100644 --- a/app/assets/stylesheets/leaflet_overrides.scss +++ b/app/assets/stylesheets/leaflet_overrides.scss @@ -1,13 +1,13 @@ .leaflet-popup-content-wrapper, .leaflet-popup-tip { - border: none; + border: 0; } .thumbnail { - background: #fff !important; - border: solid 1px whitesmoke; -} + background: $white; + border: solid 1px $white; -.thumbnail .crop-thumbnail .cropinfo { - padding-top: 14px; + .crop-thumbnail .cropinfo { + padding-top: 14px; + } } diff --git a/app/views/places/index.html.haml b/app/views/places/index.html.haml index ebc180a81..f915e67db 100644 --- a/app/views/places/index.html.haml +++ b/app/views/places/index.html.haml @@ -1,3 +1,6 @@ - content_for :title, t(".title", site_name: ENV['GROWSTUFF_SITE_NAME']) + +%h2=t(".title", site_name: ENV['GROWSTUFF_SITE_NAME']) + = render partial: 'search_form' #placesmap.map