L.esri.Geocoding.Geosearch

Extends L.Control

L.esri.Geocoding.Geosearch is a control for auto-complete enabled search. You can find more information and the source code for this plugin here.

Constructor

ConstructorDescription
L.esri.Geocoding.geosearch(<GeosearchObject>options)Creates a new Geosearch control

Options

OptionTypeDefaultDescription
positionString'topleft'Expects a valid Leaflet control position.
zoomToResultBooleantrueDetermines whether or not the map will zoom to the result after geocoding is complete.
useMapBoundsBoolean or Integer12Determines if and when the geocoder should use the bounds of the map to filter search results. If true the geocoder will always return results in the current map bounds. If false it will always search the world. If an integer (like 11) is passed, the geocoder will use the bounds of the map for searching only if the map is currently zoomed in far enough.
collapseAfterResultBooleantrueDetermines whether or not the geocoder should collapse after a result is found.
expandedBooleanfalseDetermines whether or not the geocoder starts in an expanded state.
allowMultipleResultsBooleantrueIf set to true and the user submits the form without a suggestion, the control will geocode the current text in the input.
providersArraySee descriptionAn array of providers to search.
placeholderString'Search for places or addresses'Placeholder text for the search input.
titleString'Location Search'Title text for the search input. Shows as a tooltip on hover.
searchBoundsL.latLngBoundsnullIf set, the geocoder will filter results using the provided static bounding box regardless of the current zoom level of the map, overriding useMapBounds entirely.

Events

EventDataDescription
results<ResultsEvent>Fired when results are returned from the geocoder.

Events from each provider will match the events fired by L.esri.Service.

Methods

MethodReturnsDescription
clear()this

Clears the current text and collapses the control if collapseAfterResult is true.

disable()

grays out the input so that addresses cannot be searched.

enable()

ungray the input so that addresses can once again be searched.

Styling

For reference here is the internal DOM structure of the geocoder

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
<div className="geocoder-control leaflet-control">
    <input className="geocoder-control-input leaflet-bar">
    <ul className="geocoder-control-suggestions leaflet-bar">
        <li className="geocoder-control-suggestion geocoder-control-selected">The Selected Result</li>
        <li className="geocoder-control-suggestion">Another Result</li>
    </ul>
</div>

By default, ellipses are shown when the text of suggestions is too long. However, you may want to show the entire suggestions text.

The following CSS rule will show the entire suggestions text, broken onto multiple lines. You will need to add the rule in your own custom stylesheet associated with your project:

Use dark colors for code blocksCopy
1
2
3
4
5
6
.geocoder-control-suggestions .geocoder-control-suggestion {
    overflow-wrap: break-word;
    word-break: break-all;
    overflow: visible;
    white-space: break-spaces;
}

Example

Live sample here.

Providers

The Geosearch control can also search for results from a variety of sources including Feature Layers and Map Services. This is done with plain text matching and is not "real" geocoding. But it allows you to mix custom results into a search box.

Use dark colors for code blocksCopy
1
2
<link rel="stylesheet" href="./esri-leaflet-geocoder.css">
<script src="./esri-leaflet-geocoder.js"></script>
var arcgisOnline = L.esri.Geocoding.arcgisOnlineProvider({
    // API Key to be passed to the ArcGIS Online Geocoding Service - https://developers.arcgis.com
    apikey: 'YOUR_API_KEY'
});

var gisDay = L.esri.Geocoding.featureLayerProvider({
    url: 'https://services.arcgis.com/uCXeTVveQzP4IIcx/arcgis/rest/services/GIS_Day_Final/FeatureServer/0',
    searchFields: ['Name', 'Organization'], // Search these fields for text matches
    label: 'GIS Day Events', // Group suggestions under this header
    formatSuggestion: function (feature) {
        return feature.properties.Name + ' - ' + feature.properties.Organization; // format suggestions like this.
    }
});

L.esri.Geocoding.Controls.geosearch({
    providers: [arcgisOnline, gisDay] // will geocode via ArcGIS Online and search the GIS Day feature service.
}).addTo(map);

Available Providers

  • L.esri.Geocoding.arcgisOnlineProvider uses the ArcGIS geocoding service.
  • L.esri.Geocoding.featureLayerProvider gets results by querying a Feature Layer for text matches.
  • L.esri.Geocoding.mapServiceProvider uses the find and query methods of ArcGIS Server Map Services to get text matches.
  • L.esri.Geocoding.geocodeServiceProvider uses an ArcGIS Server Geocode Service (and supports suggestions from ArcGIS Server 10.3+ enabled endpoints).

All providers share the following options:

OptionTypeDefaultDescription
labelString'Provider Type'Text that will be used to group suggestions (when more than one provider is used).
maxResultsInteger5Maximum number of results to show for the provider. Hard limit for suggestions from the ArcGIS geocoding service is 15.
attributionstringVaries by providerAdds attribution to the map to credit the source.
apikeyStringnullWill use this api key to authenticate all calls to the service.
tokenStringnullWill use this token to authenticate all calls to the service.
arcgisOnlineProvider
OptionTypeDefaultDescription
countriesString [Strings]nullLimit results to one or more countries. Any ISO 3166 2 or 3 digit country code supported by the ArcGIS World Geocode service is allowed. Note: using an array of country codes may result in inaccurate results even when a specific suggestion is supplied.
categoriesString [Strings]nullLimit results to one or more categories. See the list of valid categories for possible values.
forStorageBooleanfalseIndicates whether results will be stored permanently. More information can be found here.
Results from the arcgisOnlineProvider will have an additional properties key which will correspond with all available fields in the World Geocode service
geocodeServiceProvider
OptionTypeDefaultDescription
urlStringRequiredThe URL for the service that will be searched.
labelString'Geocode Service'Text that will be used to group suggestions under when more than one provider is being used.

Results from the geocodeServiceProvider will have an additional properties key which will correspond with all the available fields in the service.

featureLayerProvider
OptionTypeDefaultDescription
urlStringRequiredThe URL for the service that will be searched.
searchFieldsString Array[Strings]NoneAn array of fields to search for text.
formatSuggestionFunctionSee DescriptionFormatting function for the suggestion text. Receives feature information and returns a string.
bufferRadiusInteger1000If a service or layer contains points, buffer points by this radius (in meters) to create bounds.
searchModeString'contain'Sets how the search is performed against features. Options are contain, startWith, endWith, or strict. Please refer to the FeatureLayer Provider JavaScript file for how the different searches are structed.
featureLayerProvider Methods
MethodReturnsDescription
orderBy(<String>fieldname, <String>order)this

Sort output features using values from an individual field. "ASC" (ascending) is the default sort order, but "DESC" can be passed as an alternative. This method can be called more than once to apply advanced sorting.

Results from the featureLayerProvider will have an additional properties key which will contain all the information for the feature and a geojson key that will contain a GeoJSON representation of the feature.

mapServiceProvider
OptionTypeDefaultDescription
urlStringRequiredThe URL for the service that will be searched.
searchFieldsString Array[Strings]NoneAn array of fields to search for text.
layersInteger Array[Integers][0]An array of layer identifiers to find text matches on.
formatSuggestionFunctionSee DescriptionFormatting function for the suggestion text. Receives feature information and returns a string.
bufferRadiusInteger Array[Integers]Buffer point results by this radius to create bounds.

Results from the mapServiceProvider will have an additional properties key which will contain all the information for the feature and a geojson key that will contain a GeoJSON representation of the feature.

Events from each provider will match the events fired by L.esri.Service.

Results

PropertyTypeDescription
boundsL.LatLngBoundsThe bounds around this suggestion. Helpful for zooming to results like cities and states.
latlngL.LatLngThe center of the results.
textStringThe entered search text.
results[<ResultObject>]An array of result objects.

Result Object

A single result from a provider. You should not rely on all these properties being present in every result object. Some providers may add additional properties.

PropertyTypeDescription
textStringThe text that was passed to the provider.
boundsL.LatLngBoundsThe bounds around this result. Helpful for zooming to results like cities and states.
latlngL.LatLngThe center of the result.

The result object will also contain any additional properties from the provider. See the available providers for more information.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.