From 9cb819c756b0ce83f6aa4e8cda24f63bcb035bf3 Mon Sep 17 00:00:00 2001 From: Tom Brown Date: Wed, 4 Dec 2019 08:17:12 -0500 Subject: [PATCH 1/4] change to unpkg.com/leaflet-control-geocoder and opencage geocoding. hard codes my api key. --- flask_admin/static/admin/js/form.js | 32 ++----------------- .../templates/bootstrap2/admin/lib.html | 4 ++- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/flask_admin/static/admin/js/form.js b/flask_admin/static/admin/js/form.js index a562cad9b..1a08eeb93 100644 --- a/flask_admin/static/admin/js/form.js +++ b/flask_admin/static/admin/js/form.js @@ -205,37 +205,11 @@ var drawControl = new L.Control.Draw(drawOptions); map.addControl(drawControl); if (window.MAPBOX_SEARCH) { - var circle = L.circleMarker([0, 0]); - var $autocompleteEl = $(''); - var $form = $($el.get(0).form); - - $autocompleteEl.insertAfter($map); - $form.on('submit', function (evt) { - if ($autocompleteEl.is(':focus')) { - evt.preventDefault(); - return false; - } - }); - var autocomplete = new google.maps.places.Autocomplete($autocompleteEl.get(0)); - autocomplete.addListener('place_changed', function() { - var place = autocomplete.getPlace(); - var loc = place.geometry.location; - var viewport = place.geometry.viewport; - circle.setLatLng(L.latLng(loc.lat(), loc.lng())); - circle.addTo(map); - if (viewport) { - map.fitBounds([ - viewport.getNorthEast().toJSON(), - viewport.getSouthWest().toJSON(), - ]); - } - else { - map.fitBounds(circle.getBounds()); - } - }); + L.Control.geocoder({ + geocoder: L.Control.Geocoder.opencage('8b85c7d17777437980879385adfb19b5') + }).addTo(map); } - // save when the editableLayers are edited var saveToTextArea = function() { var geo = editableLayers.toGeoJSON(); diff --git a/flask_admin/templates/bootstrap2/admin/lib.html b/flask_admin/templates/bootstrap2/admin/lib.html index f05d5f93e..cea2026b7 100644 --- a/flask_admin/templates/bootstrap2/admin/lib.html +++ b/flask_admin/templates/bootstrap2/admin/lib.html @@ -245,7 +245,9 @@

{{ text }}

- + + + {% endif %} {% endif %} From 0924d56bd56135747abf90f85e53064ff084e5c3 Mon Sep 17 00:00:00 2001 From: Tom Brown Date: Sun, 26 Dec 2021 16:32:14 -0800 Subject: [PATCH 2/4] add accessToken and id as params of L.tileLayer as seen at https://docs.mapbox.com/help/troubleshooting/migrate-legacy-static-tiles-api/ --- flask_admin/static/admin/js/form.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flask_admin/static/admin/js/form.js b/flask_admin/static/admin/js/form.js index eeb5f03ae..9aedb9f11 100644 --- a/flask_admin/static/admin/js/form.js +++ b/flask_admin/static/admin/js/form.js @@ -161,7 +161,9 @@ var attribution = $el.data('tile-layer-attribution') || '' L.tileLayer('//'+$el.data('tile-layer-url'), { attribution: attribution, - maxZoom: 18 + maxZoom: 18, + accessToken: window.MAPBOX_ACCESS_TOKEN, + id: 'mapbox/streets-v11', }).addTo(map) } else { var mapboxUrl = 'https://api.mapbox.com/styles/v1/mapbox/'+window.MAPBOX_MAP_ID+'/tiles/{z}/{x}/{y}?access_token='+window.MAPBOX_ACCESS_TOKEN From 7f01b7ae56632fcc0e158926de730b9830d3730d Mon Sep 17 00:00:00 2001 From: Tom Brown Date: Sun, 26 Dec 2021 17:44:25 -0800 Subject: [PATCH 3/4] change to mapbox geocoder --- flask_admin/static/admin/js/form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_admin/static/admin/js/form.js b/flask_admin/static/admin/js/form.js index 9aedb9f11..f83ab3180 100644 --- a/flask_admin/static/admin/js/form.js +++ b/flask_admin/static/admin/js/form.js @@ -208,7 +208,7 @@ map.addControl(drawControl); if (window.MAPBOX_SEARCH) { L.Control.geocoder({ - geocoder: L.Control.Geocoder.opencage('8b85c7d17777437980879385adfb19b5') + geocoder: L.Control.Geocoder.mapbox({'apiKey': window.MAPBOX_ACCESS_TOKEN}) }).addTo(map); } From 8d747bace887785af847f1d71e89bf2ae58fb58b Mon Sep 17 00:00:00 2001 From: Tom Brown Date: Wed, 2 Feb 2022 12:13:21 -0800 Subject: [PATCH 4/4] change MAPBOX_SEARCH to LEAFLET_CONTROL_GEOCODER. Manual testing seems to work. --- examples/geo_alchemy/config.py | 2 ++ flask_admin/static/admin/js/form.js | 8 ++++---- flask_admin/templates/bootstrap2/admin/lib.html | 6 +++--- flask_admin/templates/bootstrap3/admin/lib.html | 8 +++++--- flask_admin/templates/bootstrap4/admin/lib.html | 8 +++++--- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/examples/geo_alchemy/config.py b/examples/geo_alchemy/config.py index fa0516330..872df3b24 100644 --- a/examples/geo_alchemy/config.py +++ b/examples/geo_alchemy/config.py @@ -9,6 +9,8 @@ MAPBOX_MAP_ID = '...' MAPBOX_ACCESS_TOKEN = '...' +LEAFLET_CONTROL_GEOCODER = True + # when the creating new shapes, use this default map center DEFAULT_CENTER_LAT = -33.918861 DEFAULT_CENTER_LONG = 18.423300 diff --git a/flask_admin/static/admin/js/form.js b/flask_admin/static/admin/js/form.js index f83ab3180..89ec12d7f 100644 --- a/flask_admin/static/admin/js/form.js +++ b/flask_admin/static/admin/js/form.js @@ -206,10 +206,10 @@ } var drawControl = new L.Control.Draw(drawOptions); map.addControl(drawControl); - if (window.MAPBOX_SEARCH) { - L.Control.geocoder({ - geocoder: L.Control.Geocoder.mapbox({'apiKey': window.MAPBOX_ACCESS_TOKEN}) - }).addTo(map); + if (window.LEAFLET_CONTROL_GEOCODER) { + L.Control.geocoder({ + geocoder: L.Control.Geocoder.nominatim() + }).addTo(map); } // save when the editableLayers are edited diff --git a/flask_admin/templates/bootstrap2/admin/lib.html b/flask_admin/templates/bootstrap2/admin/lib.html index cea2026b7..2f8ee6804 100644 --- a/flask_admin/templates/bootstrap2/admin/lib.html +++ b/flask_admin/templates/bootstrap2/admin/lib.html @@ -241,11 +241,11 @@

{{ text }}

- {% if config.MAPBOX_SEARCH %} + {% if config.LEAFLET_CONTROL_GEOCODER %} - + {% endif %} diff --git a/flask_admin/templates/bootstrap3/admin/lib.html b/flask_admin/templates/bootstrap3/admin/lib.html index 6bfdb05a6..282fb2659 100644 --- a/flask_admin/templates/bootstrap3/admin/lib.html +++ b/flask_admin/templates/bootstrap3/admin/lib.html @@ -232,11 +232,13 @@

{{ text }}

- {% if config.MAPBOX_SEARCH %} + {% if config.LEAFLET_CONTROL_GEOCODER %} - + + + {% endif %} {% endif %} diff --git a/flask_admin/templates/bootstrap4/admin/lib.html b/flask_admin/templates/bootstrap4/admin/lib.html index 40db5bb41..494dfa72f 100644 --- a/flask_admin/templates/bootstrap4/admin/lib.html +++ b/flask_admin/templates/bootstrap4/admin/lib.html @@ -268,11 +268,13 @@

{{ text }}

- {% if config.MAPBOX_SEARCH %} + {% if config.LEAFLET_CONTROL_GEOCODER %} - + + + {% endif %} {% endif %}