Use Google Elevation Service API to detect a place’s elevation

Google Maps API offer a lot of possibilities, especially with the new Javascript V3 API. Part of this are the so called services: geocoding, directions, elevation and places.

Elevation service allows you to get the elevation of a place given the coordinates or the place address. To use it within your google maps code, you just need to call the service:

var elevator;
var map;
var infowindow = new google.maps.InfoWindow();
var denali = new google.maps.LatLng(63.3333333, -150.5);
 
function initialize() {
  var myOptions = {
    zoom: 8,
    center: denali,
    mapTypeId: 'terrain'
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 
  // 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
  if (elevator) {
    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);
      }
    });
  }
}

This example was just copy pasted from the API reference page.

Otherwise, if you want to call the service from another kind of application you can just send the request using Google Maps API Web Services retriving the information in JSON or XML format. For instance typing the following URL I can get the altitude of Bologna:

http://maps.google.com/maps/api/elevation/xml?locations=44.49,11.34&sensor=false

You can also send the request for a list of places or for a path. This is perfect for calling the service, for instance, within a Flex application.
Using Adobe Flash Builder data/service wizard I can easily establish a Flex service to do this:

  1. Click on Data/Services tab
  2. Click on Connect to Data/Services… icon
  3. Select XML service type
  4. Check URL as XML source and anter the Google Maps API url in the URL field
  5. invoke
  6. Select Node -> Elevation, and you’re done!

You may also check if the response is positive before outputting the elevation somewhere…
This is all, have a look to the API references for more info!

Google Maps Javascript API V3 Reference
Google Maps API Web Service Reference

Google Rocks!

One Response to Use Google Elevation Service API to detect a place’s elevation

  1. Pingback: Tweets that mention Use Google Elevation Service API to detect a place’s elevation | Some Little News -- Topsy.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Home
Twitter Facebook Flickr Vimeo About me Email me