Extendable geocoder with builtin support for Nominatim, Bing, Google, Mapbox, Photon, What3Words, MapQuest, Mapzen, HERE
npm install leaflet-control-geocoder2dist folder:
HTML
`
Add the control to a map instance:
`javascript
var map = L.map('map').setView([0, 0], 2);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
L.Control.geocoder().addTo(map);
`
Customizing
By default, when a geocoding result is found, the control will center the map on it and place
a marker at its location. This can be customized by listening to the control's markgeocode
event. To remove the control's default handler for marking a result, set the option
defaultMarkGeocode to false.
For example:
`javascript
var geocoder = L.Control.geocoder({
defaultMarkGeocode: false
})
.on('markgeocode', function(e) {
var bbox = e.geocode.bbox;
var poly = L.polygon([
bbox.getSouthEast(),
bbox.getNorthEast(),
bbox.getNorthWest(),
bbox.getSouthWest()
]).addTo(map);
map.fitBounds(poly.getBounds());
})
.addTo(map);
`
This will add a polygon representing the result's boundingbox when a result is selected.
API
L.Control.Geocoder
This is the geocoder control. It works like any other Leaflet control, and is added to the map.
$3
`js
L.Control.Geocoder(options)
`
$3
| Option | Type | Default | Description |
| --------------- | ---------------- | ----------------- | ----------- |
| collapsed | Boolean | true | Collapse control unless hovered/clicked |
| position | String | "topright" | Control position |
| placeholder | String | "Search..." | Placeholder text for text input
| removeMarker | Boolean | false | Show remove marker linkage |
| removeMarkerHTML| String | 'Remove marker' | Remove marker linkage, which shows in Marker popup |
| errorMessage | String | "Nothing found." | Message when no result found / geocoding error occurs |
| geocoder | IGeocoder | new L.Control.Geocoder.Nominatim() | Object to perform the actual geocoding queries |
| showResultIcons | Boolean | false | Show icons for geocoding results (if available); supported by Nominatim |
| draggable | Boolean | true | Allows to drag default marker to clarify position. |
$3
| Method | Returns | Description |
| ------------------------------------- | ------------------- | ----------------- |
| markGeocode( result) | this | Marks a geocoding result on the map |
L.Control.Geocoder.Nominatim
Uses Nominatim to respond to geocoding queries. This is the default
geocoding service used by the control, unless otherwise specified in the options. Implements `IGeocoder`.
Unless using your own Nominatim installation, please refer to the Nominatim usage policy.
$3
`js
L.Control.Geocoder.Nominatim(options)
`
Options
| Option | Type | Default | Description |
| --------------- | ---------------- | ----------------- | ----------- |
| serviceUrl | String | "http://nominatim.openstreetmap.org/" | URL of the service |
| geocodingQueryParams | Object | {} | Additional URL parameters (strings) that will be added to geocoding requests; can be used to restrict results to a specific country for example, by providing the countrycodes parameter to Nominatim |
| reverseQueryParams | Object | {} | Additional URL parameters (strings) that will be added to reverse geocoding requests |
| htmlTemplate | function | special | A function that takes an GeocodingResult as argument and returns an HTML formatted string that represents the result. Default function breaks up address in parts from most to least specific, in attempt to increase readability compared to Nominatim's naming
L.Control.Geocoder.Bing
Uses Bing Locations API to respond to geocoding queries. Implements `IGeocoder`.
Note that you need an API key to use this service.
$3
`
L.Control.Geocoder.Bing( key)
``