Class Geocoder
Stay organized with collections
Save and categorize content based on your preferences.
AI-generated Key Takeaways
-
Geocoder converts between addresses and geographical coordinates.
-
The
geocode(address)method gets geographic points for an address. -
The
reverseGeocode(latitude, longitude)method gets addresses for a geographic point. -
Methods like
setBounds,setLanguage, andsetRegioncan refine geocoding results.
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. constresponse=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. constdoc=DocumentApp.create('My Map'); constmap=Maps.newStaticMap(); // Add each result to the map and doc. for(leti=0;i < response.results.length && i < 9;i++){ constresult=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
| Method | Return type | Brief description |
|---|---|---|
geocode(address) | Object | Gets the approximate geographic points for a given address. |
reverse | Object | Gets the approximate addresses for a given geographic point. |
set | Geocoder | Sets the bounds of an area that should be given extra preference in the results. |
set | Geocoder | Sets the language to be used in the results. |
set | Geocoder | Sets 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. constresponse=Maps.newGeocoder().geocode('Times Square, New York, NY'); for(leti=0;i < response.results.length;i++){ constresult=response.results[i]; Logger.log( '%s: %s, %s', result.formatted_address, result.geometry.location.lat, result.geometry.location.lng, ); }
Parameters
| Name | Type | Description |
|---|---|---|
address | String | an 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. constresponse=Maps.newGeocoder().reverseGeocode(40.758577,-73.984464); for(leti=0;i < response.results.length;i++){ constresult=response.results[i]; Logger.log( '%s: %s, %s', result.formatted_address, result.geometry.location.lat, result.geometry.location.lng, ); }
Parameters
| Name | Type | Description |
|---|---|---|
latitude | Number | the latitude of the point |
longitude | Number | the longitude of the point |
Return
Object — a JSON Object containing the reverse geocoding data, as described here
See also
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. constgeocoder=Maps.newGeocoder().setBounds( 40.699642, -74.021072, 40.877569, -73.908548, );
Parameters
| Name | Type | Description |
|---|---|---|
sw | Number | the latitude of the south west corner of the bounds |
sw | Number | the longitude of the south west corner of the bounds |
ne | Number | the latitude of the north east corner of the bounds |
ne | Number | the 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. constgeocoder=Maps.newGeocoder().setLanguage('fr');
Parameters
| Name | Type | Description |
|---|---|---|
language | String | a 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. constgeocoder=Maps.newGeocoder().setRegion('fr');
Parameters
| Name | Type | Description |
|---|---|---|
region | String | the region code to use |
Return
Geocoder — the Geocoder object to facilitate chaining of calls