Overview
The Elevation service provides elevation data for locations on the surface of the earth, including depth locations on the ocean floor (which return negative values). In those cases where Google does not possess exact elevation measurements at the precise location you request, the service will interpolate and return an averaged value using the four nearest locations.
The ElevationService
object provides you with
a simple interface to query locations on the earth for elevation
data. Additionally, you may request sampled elevation data along
paths, allowing you to calculate the equidistant elevation changes
along routes. The ElevationService
object communicates
with the Google Maps API Elevation Service which receives elevation
requests and returns elevation data.
Note that these requests are rate-limited to discourage abuse of the service. If instead you wish to calculate elevations for static, known locations, see the Elevation Web Service documentation.
With the Elevation service, you can develop hiking and biking applications, mobile positioning applications, or low resolution surveying applications.
Elevation Requests
Accessing the Elevation service is asynchronous, since
the Google Maps API needs to make a call to an external
server. For that reason, you need to pass a callback
method to execute upon completion of the request. This
callback method should process the result(s). Note that the
Elevation service returns a status code
(ElevationStatus
) and an array of separate
ElevationResult
objects.
The ElevationService
handles two types of requests:
- Requests for separate, discrete locations using the
getElevationForLocations()
method, which is passed a list of one or more locations using aLocationElevationRequest
object. - Requests for elevation on a series of connected points along
a path using the
getElevationAlongPath()
method, which is passed an ordered set of path vertices within aPathElevationRequest
object. When requesting elevations along paths, you must also pass a parameter indicating how many samples you wish to take along that path.
Each of these methods must also pass a callback
method to handle the returned ElevationResult
and ElevationStatus
objects.
Location Elevation Requests
A LocationElevationRequest
object literal
contains the following field:
{ locations[]: LatLng }
locations
(required) defines the location(s) on the earth
from which to return elevation data. This parameter takes an array of
LatLng
s.
Sampled Path Elevation Requests
A PathElevationRequest
object literal
contains the following fields:
{ path[]: LatLng, samples: Number }
These fields are explained below:
path
(required) defines a path on the earth for which to return elevation data. Thepath
parameter defines a set of two or more ordered {latitude,longitude} pairs using an array of two or moreLatLng
objects.samples
(required) specifies the number of sample points along a path for which to return elevation data. Thesamples
parameter divides the givenpath
into an ordered set of equidistant points along the path.
As with positional requests, the path
parameter
specifies a set of latitude and longitude values. Unlike a positional
request, however, the path
specifies an ordered set of
vertices. Rather than return elevation data at the vertices, path
requests are sampled along the length of the path, where each
sample is equidistant from each other (inclusive of the endpoints).
Elevation Responses
For each valid request, the Elevation service will return
to the defined callback a set of ElevationResult
objects along with an ElevationStatus
object.
Elevation Statuses
Each elevation request returns an ElevationStatus
code
within its callback function. This status
code
will contain one of the following values:
OK
indicating the service request was successfulINVALID_REQUEST
indicating the service request was malformedOVER_QUERY_LIMIT
indicating that the requestor has exceeded quotaREQUEST_DENIED
indicating the service did not complete the request, likely because on an invalid parameterUNKNOWN_ERROR
indicating an unknown error
You should check that your callback succeeded by examining this
status code for google.maps.ElevationStatus.OK
.
Elevation Results
Upon success, the results
argument of your callback
function will contain a set of ElevationResult
objects.
These objects contain the following elements:
- A
location
element (containingLatLng
objects) of the position for which elevation data is being computed. Note that for path requests, the set oflocation
elements will contain the sampled points along the path. - An
elevation
element indicating the elevation of the location in meters. - A
resolution
value, indicating the maximum distance between data points from which the elevation was interpolated, in meters. This property will be missing if the resolution is not known. Note that elevation data becomes more coarse (largerresolution
values) when multiple points are passed. To obtain the most accurate elevation value for a point, it should be queried independently.
Elevation Examples
The following code translates a click on a map into an elevation
request using the LocationElevationRequest
object:
var elevator; var map; var infowindow = new google.maps.InfoWindow(); var denali = new google.maps.LatLng(63.3333333, -150.5); function initialize() { var mapOptions = { zoom: 8, center: denali, mapTypeId: 'terrain' } map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); // Create an ElevationService elevator = new google.maps.ElevationService(); // Add a listener for the click event and call getElevation on that location google.maps.event.addListener(map, 'click', getElevation); } function getElevation(event) { var locations = []; // Retrieve the clicked location and push it on the array var clickedLocation = event.latLng; locations.push(clickedLocation); // Create a LocationElevationRequest object using the array's one value var positionalRequest = { 'locations': locations } // Initiate the location request elevator.getElevationForLocations(positionalRequest, function(results, status) { if (status == google.maps.ElevationStatus.OK) { // Retrieve the first result if (results[0]) { // Open an info window indicating the elevation at the clicked position infowindow.setContent("The elevation at this point
is " + results[0].elevation + " meters."); infowindow.setPosition(clickedLocation); infowindow.open(map); } else { alert("No results found"); } } else { alert("Elevation service failed due to: " + status); } }); }
View example (elevation-simple.html)
The following example constructs a polyline given a set of coordinates
and displays elevation data along that path using the
Google Visualization API. (You must load this API using the Google Common
Loader.) An elevation request is constructed using the
PathElevationRequest
:
var elevator; var map; var chart; var infowindow = new google.maps.InfoWindow(); var polyline; // The following path marks a general path from Mt. // Whitney, the highest point in the continental United // States to Badwater, Death Vallet, the lowest point. var whitney = new google.maps.LatLng(36.578581, -118.291994); var lonepine = new google.maps.LatLng(36.606111, -118.062778); var owenslake = new google.maps.LatLng(36.433269, -117.950916); var beattyjunction = new google.maps.LatLng(36.588056, -116.943056); var panamintsprings = new google.maps.LatLng(36.339722, -117.467778); var badwater = new google.maps.LatLng(36.23998, -116.83171); // Load the Visualization API and the columnchart package. google.load("visualization", "1", {packages: ["columnchart"]}); function initialize() { var mapOptions = { zoom: 8, center: lonepine, mapTypeId: 'terrain' } map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); // Create an ElevationService. elevator = new google.maps.ElevationService(); // Draw the path, using the Visualization API and the Elevation service. drawPath(); } function drawPath() { // Create a new chart in the elevation_chart DIV. chart = new google.visualization.ColumnChart(document.getElementById('elevation_chart')); var path = [ whitney, lonepine, owenslake, panamintsprings, beattyjunction, badwater]; // Create a PathElevationRequest object using this array. // Ask for 256 samples along that path. var pathRequest = { 'path': path, 'samples': 256 } // Initiate the path request. elevator.getElevationAlongPath(pathRequest, plotElevation); } // Takes an array of ElevationResult objects, draws the path on the map // and plots the elevation profile on a Visualization API ColumnChart. function plotElevation(results, status) { if (status == google.maps.ElevationStatus.OK) { elevations = results; // Extract the elevation samples from the returned results // and store them in an array of LatLngs. var elevationPath = []; for (var i = 0; i < results.length; i++) { elevationPath.push(elevations[i].location); } // Display a polyline of the elevation path. var pathOptions = { path: elevationPath, strokeColor: '#0000CC', opacity: 0.4, map: map } polyline = new google.maps.Polyline(pathOptions); // Extract the data from which to populate the chart. // Because the samples are equidistant, the 'Sample' // column here does double duty as distance along the // X axis. var data = new google.visualization.DataTable(); data.addColumn('string', 'Sample'); data.addColumn('number', 'Elevation'); for (var i = 0; i < results.length; i++) { data.addRow(['', elevations[i].elevation]); } // Draw the chart using the data within its DIV. document.getElementById('elevation_chart').style.display = 'block'; chart.draw(data, { width: 640, height: 200, legend: 'none', titleY: 'Elevation (m)' }); } }