Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change map search to leaflet-control-geocoder with Nominatim #2221

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/geo_alchemy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 7 additions & 31 deletions flask_admin/static/admin/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -204,38 +206,12 @@
}
var drawControl = new L.Control.Draw(drawOptions);
map.addControl(drawControl);
if (window.MAPBOX_SEARCH) {
var circle = L.circleMarker([0, 0]);
var $autocompleteEl = $('<input style="position: absolute; z-index: 9999; display: block; margin: -42px 0 0 10px; width: 50%">');
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());
}
});
if (window.LEAFLET_CONTROL_GEOCODER) {
L.Control.geocoder({
geocoder: L.Control.Geocoder.nominatim()
}).addTo(map);
}


// save when the editableLayers are edited
var saveToTextArea = function() {
var geo = editableLayers.toGeoJSON();
Expand Down
8 changes: 5 additions & 3 deletions flask_admin/templates/bootstrap2/admin/lib.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,13 @@ <h3>{{ text }}</h3>
</script>
<script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.js', v='1.0.2') }}"></script>
<script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.draw.js', v='0.4.6') }}"></script>
{% if config.MAPBOX_SEARCH %}
{% if config.LEAFLET_CONTROL_GEOCODER %}
<script>
window.MAPBOX_SEARCH = "{{ config.MAPBOX_SEARCH }}";
window.LEAFLET_CONTROL_GEOCODER = "{{ config.LEAFLET_CONTROL_GEOCODER }}";
</script>
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&key={{ config.get('GOOGLE_MAPS_API_KEY') }}"></script>
<!-- Suggested by https://www.npmjs.com/package/leaflet-control-geocoder#usage -->
<link rel="stylesheet" href="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css" />
<script src="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js"></script>
{% endif %}
{% endif %}
<script src="{{ admin_static.url(filename='vendor/bootstrap-daterangepicker/daterangepicker.js', v='1.3.22') }}"></script>
Expand Down
8 changes: 5 additions & 3 deletions flask_admin/templates/bootstrap3/admin/lib.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@ <h3>{{ text }}</h3>
</script>
<script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.js', v='1.0.2') }}"></script>
<script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.draw.js', v='0.4.6') }}"></script>
{% if config.MAPBOX_SEARCH %}
{% if config.LEAFLET_CONTROL_GEOCODER %}
<script>
window.MAPBOX_SEARCH = "{{ config.MAPBOX_SEARCH }}";
window.LEAFLET_CONTROL_GEOCODER = "{{ config.LEAFLET_CONTROL_GEOCODER }}";
</script>
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&key={{ config.get('GOOGLE_MAPS_API_KEY') }}"></script>
<!-- Suggested by https://www.npmjs.com/package/leaflet-control-geocoder#usage -->
<link rel="stylesheet" href="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css" />
<script src="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js"></script>
{% endif %}
{% endif %}
<script src="{{ admin_static.url(filename='vendor/bootstrap-daterangepicker/daterangepicker.js', v='1.3.22') }}"></script>
Expand Down
8 changes: 5 additions & 3 deletions flask_admin/templates/bootstrap4/admin/lib.html
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,13 @@ <h3>{{ text }}</h3>
</script>
<script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.js', v='1.0.2') }}"></script>
<script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.draw.js', v='0.4.6') }}"></script>
{% if config.MAPBOX_SEARCH %}
{% if config.LEAFLET_CONTROL_GEOCODER %}
<script>
window.MAPBOX_SEARCH = "{{ config.MAPBOX_SEARCH }}";
window.LEAFLET_CONTROL_GEOCODER = "{{ config.LEAFLET_CONTROL_GEOCODER }}";
</script>
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&key={{ config.get('GOOGLE_MAPS_API_KEY') }}"></script>
<!-- Suggested by https://www.npmjs.com/package/leaflet-control-geocoder#usage -->
<link rel="stylesheet" href="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css" />
<script src="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js"></script>
{% endif %}
{% endif %}
<script src="{{ admin_static.url(filename='vendor/bootstrap-daterangepicker/daterangepicker.js', v='1.3.22') }}"></script>
Expand Down