Search code examples
django-leaflet

how to add leaflet-geosearch control in django admin


beginner in programming, for a django(2.1) project i have added a leaflet-geosearch control on a map. It works fine but my need is to have this control in admin(LeafletGeoAdmin). I absolutely don't know how to do that. Thanks to anyone who can help me with this.

Here is what i have added in my template to displate the map with the geosearch control:

... ....

<div id="leafleft_container">
{% leaflet_map "map" callback="mapInit" %}
<script type="text/javascript">
var GeoSearchControl = window.GeoSearch.GeoSearchControl;

var OpenStreetMapProvider = window.GeoSearch.OpenStreetMapProvider;

var provider = new OpenStreetMapProvider();

var searchControl = new GeoSearchControl({
 provider: provider,
 searchLabel: 'Lieu à rechercher',
 notFoundMessage: 'Non trouvé',
 retainZoomLevel: true,
 showMarker: true,
 selected: 0,
 autoClose:true,
 keepResult:true

});
var map = L.map('map');
var osmUrl='https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png';
var osmAttrib='Map data © OpenStreetMap';
var osm = new L.TileLayer(osmUrl, {attribution: osmAttrib});
map.setView({{Centrage}}, 4);
map.addLayer(osm);
map.addControl(searchControl);

... I guess to have the same result in admin I should add the same somewhere in leaflet widget but I have no idea where.


Solution

  • one solution was to add the leaflet-geosearch code into leaflet-extrajs and link the CDN for leaflet geo-search into widget.html. Not sure it is the best practice but it works.