javascripthtmlleafletgeocoder

How Can I work with Leaflet Control Geocode?


I plan to add a leaflet control geocoder or a search box to my app. Although I wrote the needed code it doesn't display any geocoder and I don't know what is wrong. I have provided a part of my index.html file including related code.

<link rel="stylesheet" href="{% static 'geocoder/Control.Geocoder.css' %}" />
<script type="text/javascript" src="{% static 'geocoder/Control.Geocoder.js' %}"></script>

<script>
var geocoder = L.Control.geocoder().on('markgeocode', function(event) {
     var center = event.geocode.center;
     L.marker(center).addTo(map);
     map.setView(center, map.getZoom());
}).addTo(map);
</script>

Solution

  • You have to set the geocode "type": Types

    var _geocoderType = L.Control.Geocoder.nominatim();
    var geocoder = L.Control.geocoder({
       geocoder: _geocoderType
    }).addTo(map);
    
    geocoder.on('markgeocode', function(event) {
         var center = event.geocode.center;
         L.marker(center).addTo(map);
         map.setView(center, map.getZoom());
    });