From 828e9d318a9b1a9dc794bfd81be883ecebf248fb Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 4 Jul 2014 11:02:23 +1000 Subject: [PATCH] check that map exists on page before drawing it --- app/assets/javascripts/crops.js.erb | 12 ++++---- app/assets/javascripts/places.js.erb | 44 +++++++++++++++------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/crops.js.erb b/app/assets/javascripts/crops.js.erb index 2b3e667d1..3a3837633 100644 --- a/app/assets/javascripts/crops.js.erb +++ b/app/assets/javascripts/crops.js.erb @@ -1,10 +1,12 @@ -mapbox_map_id = "<%= Growstuff::Application.config.mapbox_map_id %>"; -mapbox_base_url = "https://c.tiles.mapbox.com/v3/" + mapbox_map_id + "/{z}/{x}/{y}.png"; +if (document.getElementById("cropmap") !== null) { + mapbox_map_id = "<%= Growstuff::Application.config.mapbox_map_id %>"; + mapbox_base_url = "https://c.tiles.mapbox.com/v3/" + mapbox_map_id + "/{z}/{x}/{y}.png"; -L.Icon.Default.imagePath = '/assets' + L.Icon.Default.imagePath = '/assets' -cropmap = L.map('cropmap').setView([0.0, -0.0], 2); -showCropMap(cropmap); + cropmap = L.map('cropmap').setView([0.0, -0.0], 2); + showCropMap(cropmap); +} function showCropMap(cropmap) { diff --git a/app/assets/javascripts/places.js.erb b/app/assets/javascripts/places.js.erb index 5ac8f128d..7b4bff9de 100644 --- a/app/assets/javascripts/places.js.erb +++ b/app/assets/javascripts/places.js.erb @@ -1,27 +1,29 @@ -places_base_path = "/places"; -mapbox_map_id = "<%= Growstuff::Application.config.mapbox_map_id %>"; -mapbox_base_url = "https://c.tiles.mapbox.com/v3/" + mapbox_map_id + "/{z}/{x}/{y}.png"; -nominatim_base_url = 'http://nominatim.openstreetmap.org/search/'; -nominatim_user_agent_email = "<%= Growstuff::Application.config.user_agent_email %>"; +if (document.getElementById("placesmap") !== null) { + places_base_path = "/places"; + mapbox_map_id = "<%= Growstuff::Application.config.mapbox_map_id %>"; + mapbox_base_url = "https://c.tiles.mapbox.com/v3/" + mapbox_map_id + "/{z}/{x}/{y}.png"; + nominatim_base_url = 'http://nominatim.openstreetmap.org/search/'; + nominatim_user_agent_email = "<%= Growstuff::Application.config.user_agent_email %>"; -L.Icon.Default.imagePath = '/assets' + L.Icon.Default.imagePath = '/assets' -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 = { - format: "json", - callback: "placeholder", - limit: 1, - email: nominatim_user_agent_email - }; - $.getJSON(nominatim_query_url, nominatim_options, function(data) { - placesmap = L.map('map').setView([data[0].lat, data[0].lon], 5); + 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 = { + format: "json", + callback: "placeholder", + limit: 1, + email: nominatim_user_agent_email + }; + $.getJSON(nominatim_query_url, nominatim_options, function(data) { + placesmap = L.map('map').setView([data[0].lat, data[0].lon], 5); + showMap(placesmap); + }) + } } function showMap(placesmap) {