Google Apps Script

Class Geocoder

Allows for the conversion between an address and geographical coordinates.
The example below shows how you can use this class find the top nine matches for the location "Main St" in Colorado, add them to a map, and then embed it in a new Google Doc.

 
// Find the best matches for "Main St" in Colorado.
 var response = Maps.newGeocoder()
     // The latitudes and longitudes of southwest and northeast corners of Colorado, respectively.
     .setBounds(36.998166, -109.045486, 41.001666,-102.052002)
     .geocode('Main St');

 // Create a Google Doc and map.
 var doc = DocumentApp.create('My Map');
 var map = Maps.newStaticMap();

 // Add each result to the map and doc.
 for (var i = 0; i < response.results.length && i < 9; i++) {
   var result = response.results[i];
   map.setMarkerStyle(null, null, i + 1);
   map.addMarker(result.geometry.location.lat, result.geometry.location.lng);
   doc.appendListItem(result.formatted_address);
 }

 // Add the finished map to the doc.
 doc.appendImage(Utilities.newBlob(map.getMapImage(), 'image/png'));
 

See also

Methods

MethodReturn typeBrief description
geocode(address)ObjectGets the approximate geographic points for a given address.
reverseGeocode(latitude, longitude)ObjectGets the approximate addresses for a given geographic point.
reverseGeocode(swLatitude, swLongitude, neLatitude, neLongitude)ObjectGets the approximate addresses for a given area.
setBounds(swLatitude, swLongitude, neLatitude, neLongitude)GeocoderSets the bounds of an area that should be given extra preference in the results.
setLanguage(language)GeocoderSets the language to be used in the results.
setRegion(region)GeocoderSets a region to use when interpreting location names.

Detailed documentation

geocode(address)

Gets the approximate geographic points for a given address.

 
// Gets the geographic coordinates for Times Square.
 var response = Maps.newGeocoder().geocode('Times Square, New York, NY');
 for (var i = 0; i < response.results.length; i++) {
   var result = response.results[i];
   Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat,
       result.geometry.location.lng);
 }
 

Parameters

NameTypeDescription
addressStringan address

Return

Object — a JSON Object containing the geocoding data, as described here


reverseGeocode(latitude, longitude)

Gets the approximate addresses for a given geographic point.

 
// Gets the address of a point in Times Square.
 var response = Maps.newGeocoder().reverseGeocode(40.758577, -73.984464);
 for (var i = 0; i < response.results.length; i++) {
   var result = response.results[i];
   Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat,
       result.geometry.location.lng);
 }
 

Parameters

NameTypeDescription
latitudeNumberthe latitude of the point
longitudeNumberthe longitude of the point

Return

Object — a JSON Object containing the reverse geocoding data, as described here

See also


reverseGeocode(swLatitude, swLongitude, neLatitude, neLongitude)

Gets the approximate addresses for a given area.

 
// Gets the address of an area around Central Park.
 var response = Maps.newGeocoder().reverseGeocode(40.764941, -73.98262, 40.799133, -73.947558);
 for (var i = 0; i < response.results.length; i++) {
   var result = response.results[i];
   Logger.log('%s: %s, %s', result.formatted_address, result.geometry.location.lat,
       result.geometry.location.lng);
 }
 

Parameters

NameTypeDescription
swLatitudeNumberthe latitude of the south west corner of the bounds
swLongitudeNumberthe longitude of the south west corner of the bounds
neLatitudeNumberthe latitude of the north east corner of the bounds
neLongitudeNumberthe longitude of the north east corner of the bounds

Return

Object — a JSON Object containing the reverse geocoding data, as described here


setBounds(swLatitude, swLongitude, neLatitude, neLongitude)

Sets the bounds of an area that should be given extra preference in the results.

 
// Creates a Geocoder that prefers points in the area of Manhattan.
 var geocoder = Maps.newGeocoder()
     .setBounds(40.699642, -74.021072, 40.877569, -73.908548);
 

Parameters

NameTypeDescription
swLatitudeNumberthe latitude of the south west corner of the bounds
swLongitudeNumberthe longitude of the south west corner of the bounds
neLatitudeNumberthe latitude of the north east corner of the bounds
neLongitudeNumberthe longitude of the north east corner of the bounds

Return

Geocoder — the Geocoder object to facilitate chaining of calls

See also


setLanguage(language)

Sets the language to be used in the results.

 
// Creates a Geocoder with the language set to French.
 var geocoder = Maps.newGeocoder().setLanguage('fr');
 

Parameters

NameTypeDescription
languageStringa BCP-47 language identifier

Return

Geocoder — the Geocoder object to facilitate chaining of calls.

See also


setRegion(region)

Sets a region to use when interpreting location names. The supported region codes correspond to the ccTLDs supported by Google Maps. For example, the region code "uk" corresponds to "maps.google.co.uk".

 
// Creates a Geocoder with the region set to France.
 var geocoder = Maps.newGeocoder().setRegion('fr');
 

Parameters

NameTypeDescription
regionStringthe region code to use

Return

Geocoder — the Geocoder object to facilitate chaining of calls

See also

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.