MapQuest-GL.js

mqgl.Map.geocode

The geocode object performing geocoding, batch geocoding and reverse geocoding on the map.

Methods

mqgl.Map.geocode(location, options)

Converts a string location into a pair of geocodes. Optionally adds a marker on the map.

Syntax

Simple
var map = new mqgl.Map('map', 'KEY');

map.load( () => {
  map.geocode('Denver, CO', {addMarker: true})
    .then( () => map.fitBounds() );
});
Advanced
var map = new mqgl.Map('map', 'KEY');

map.load( () => {
  map.geocode('1555 Blake St, Denver, CO', {addMarker: true})
    .then( () => {
      return map.geocode('Boulder, CO')
    })
    .then( point => {
      map.icons.marker.add(point, 'boulder', 'marker-sm-blue.png');
    })
    .then( () => map.fitBounds() );
});

Parameters

  • location required string

    A single location to be geocoded. Advanced Location Objects are also supported.

    Example: Single Line Location

    'Washington, DC'

    Example: Advanced Location Object
    {
      location: {
        street: '1555 Blake St',
        city: 'Denver',
        county: 'Denver',
        state: 'CO'
      }
    }
  • options object

    In addition to the options from the Geocoding API, the "addMarker" option (boolean, default is false) determines if a marker should be added after the location is geocoded.

Return Value

A Promise, consisting of a single geocode object: {lng, lat}

Visual Example

mqgl.Map.batchGeocode(locations, options)

Converts an array of string locations into pairs of geocodes. Optionally adds a marker on the map.

Syntax

Simple
var map = new mqgl.Map('map', 'KEY');

map.load( () => {
  map.batchGeocode(['Denver, CO', 'Boulder, CO', 'Fort Collins, CO'], {addMarker: true})
    .then( () => map.fitBounds() );
});
Advanced
var map = new mqgl.Map('map', 'KEY');

map.load( () => {
  map.batchGeocode(['Denver, CO', 'Boulder, CO', 'Fort Collins, CO'])
    .then( points => points.forEach(
       (point, key) => map.icons.marker.add(point, 'marker-' + key)
     ))
    .then( () => map.fitBounds() );
});

Parameters

  • locations required array of strings

    Multiple locations to be geocoded. Advanced Location Objects are also supported.

    Example: Multiple Single Line Locations

    ['Washington, DC', 'New York, New York']

    Example: Advanced Location Object
    {
      locations: [
        {
          street: '1555 Blake St',
          city: 'Denver',
          county: 'Denver',
          state: 'CO'
        },
        {
          city: 'Boulder',
          state: 'CO'
        }
      ]
    }
  • options object

    In addition to the options from the Batch Geocoding API, the "addMarker" option (boolean, default is false) determines if a marker should be added after each location is geocoded.

Return Value

A Promise, consisting of an array of geocoding objects: [{lng, lat}, {lng, lat}]

mqgl.Map.reverseGeocode(geocodes, options)

Converts a pair of geocodes into an address. Optionally adds a marker on the map.

Syntax

var map = new mqgl.Map('map', 'KEY');

map.load( () => {
  map.reverseGeocode([-105.122870, 39.666076], {addMarker: true})
    .then( () => map.fitBounds() );
});

Parameters

  • geocodes required array [lng, lat]

    A single geocode to be reverse geocoded.

    Example: Single Geocode

    [-105.12287074707808, 39.66607663574351]

  • options object

    In addition to the options from the Reverse Geocoding API, the "addMarker" option (boolean, default is false) determines if a marker should be added after the location is geocoded.

Return Value

A Promise, containing the reverse geocoding response object.