Leaflet Control Geocodcer API reference

L.Control

Geocoder

This is the geocoder control. It works like any other Leaflet control, and is added to the map.

Usage example

 var map = L.map('map').setView([0, 0], 2);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    L.Control.geocoder().addTo(map);

Creation

Factory Description
L.Control.geocoder(<Geocoder options> options?) Instantiates a new geocoder control, by default using Nominatim for search, unless another
geocoder is specified in the options.

Options

Option Type Default Description
showResultIcon Boolean false Enable icons describing result type, if available
collapsed Boolean true Search field is collapsed (minimized) by default
expand String "click" Method used for expanding the control; should be "click" or "hover"
position String "topright" Sets the control's position, see Leaflet's Control docs
placeholder String "Search..." Search field's placeholder text
errorMessage String "Nothing found." Message shown when an error occurs, or no results are found
geocoder IGeocoder L.Routing.Geocoder.nominatim() Geocoder backend to use when searching

Methods

Method Returns Description
markGeocode(<IGeocodeResult> result) Called when a geocoding result is selected by the user. Default
implementation opens a marker on the result's location and opens a popup with the result's name.

IGeocoder

An interface implemented to respond to geocoding queries.

Methods

Method Returns Description
geocode(<String> query, <Function> callback, <Object> context?) Performs a geocoding query (search). When the search completes, the callback is called
with an array of IGeocodeResults as argument
reverse(<L.LatLng> latLng, <Function> callback, <Object> context?) Tries to find locations close to the coordinate latLng. When the search completes,
the callback is called with an array of IGeocodeResults as argument.
suggest(<String> query, <Function> callback, <Object> context?) Tries to find suggestions given a (possibly incomplete) search query. When the search completes,
the callback is called with an array of IGeocodeResults as argument. This method is optional to implement.

IGeocodeResult

An object that represents a result from a geocoding query. Note that there is not concrete implementation of IGeocodeResult, it is the contract (interface) geocoders are expected to use when returning results.

Properties

Property Type Description
name String Name if found location
bounds L.LatLngBounds Bounds of the location (possibly a single point)
center L.LatLng Center coordinate of the location
icon? String URL for icon representing result; optional
html? String HTML formatted representation of the name

Nominatim

Geocoder using OpenStreetMap's Nominatim service.

Options

Option Type Default Description
serviceUrl String '//nominatim.openstreetmap.org/' URL used to locate the Nominatim service
geocodingQueryParams Object {} Parameters added to each geocoding request
reverseQueryParams Object {} Parameters added to each reverse geocoding request
htmlTemplate Function Function used to generate the HTML representation of a geocoding response;
the function will be passed a parsed Nominatim geocoding response and should return a String