1.1.0 • Published 5 years ago

@maptiler/geocoder v1.1.0

Weekly downloads
414
License
BSD-2-Clause
Repository
github
Last release
5 years ago

MapTiler Geocoder

JavaScript component for geographical search provided as part of MapTiler Cloud.

Usage example

Autocomplete

<html>
  <head>
    <script src="https://cdn.maptiler.com/maptiler-geocoder/v1.1.0/maptiler-geocoder.js"></script>
    <link href="https://cdn.maptiler.com/maptiler-geocoder/v1.1.0/maptiler-geocoder.css" rel="stylesheet" />
  </head>
  <body>
    <input id="search" type="text" />
    <script>
      var geocoder = new maptiler.Geocoder({
        input: 'search', // id of input element
        key: 'get_your_own_key'
      });
      geocoder.on('select', function(item) {
        console.log('Selected', item);
      });
    </script>
  </body>
</html>

API

var geocoder = new maptiler.Geocoder({
  key: 'get_your_own_key'
});

geocoder.geocode('Zurich')
.then(function(results) {
  console.log(results.features[0]);
});

Note: You can get your own keys for free at https://cloud.maptiler.com.

API

maptiler.Geocoder

Emits: hover, select

Constructor

new Geocoder(options)

ParamTypeDefaultDescription
optionsObject
options.keystringAccess key from https://cloud.maptiler.com/
options.inputstring | HTMLInputElement | nullnullIf no input is provided, the autocomplete will not be initiated, but the rest of the methods can still be used. Most notably .geocode(query).
options.autocompleteWaitMsnumber500Number of milliseconds to wait before autocompleting. At most one request will be sent within this timeframe. Useful if you want to wait for the user to finish typing. Does not have any effect if input is not specified.
options.languagestring | Array.<string>nullSpecifies language preference e.g. en,de, null to disable.
options.boundsArray.<number>nullSearch only within the specified bounds [minx, miny, maxx, maxy], null to disable.
options.proximityArray.<number>nullPrefer results closer to the specified point [lon, lat], null to disable.

Methods

geocoder.setLanguage(language)

ParamType
languagestring | Array.<string> | null

geocoder.setBounds(bbox)

ParamType
bboxArray.<number> | null

geocoder.setProximity(point)

ParamType
pointArray.<number> | null

geocoder.getQueryUrl(query) ⇒ string

ParamType
querystring

geocoder.geocode(query) ⇒ Promise.<GeocodingResult>

Performs the search with the given query and currently set geocoder options.

ParamType
querystring

Events

The Geocoder inherits from EventEmitter, so you can use the related methods. Most notably geocoder.on (see EventEmitter#on) to listen for events:

geocoder.on('select', function(item) {
  console.log('Selected', item);
});

Possible event types:

select

Emitted when user selects an item.

hover

Emitted when user hovers over and item in the autocomplete list.

Types

GeocodingResult : GeoJSONFeatureCollection

The result of the geocoding is always a GeoJSON with individual items in a FeatureCollection at the root (features property).

Properties

NameType
featuresArray.<GeocodingResultItem>

GeocodingResultItem : GeoJSONFeature

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago