Some Little News

  • Home
  • Contact Me
  • Categories
    • css
    • flash
    • Flex 4
    • General
    • JavaScript
    • Panorama
    • Personal
    • PHP
    • Reflections
    • Resources
    • Tips&Tricks
    • Web resources
    • Web services
  • teocomi's VR
  • teocomi.com
  • teocomi Flash
  • Subscribe via RSS

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

June 28th, 2010  |  Published in Flex 4, Tips&Tricks, Web services  |  1 Comment

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!



Share this post on:
twitter facebook stumbleupon reddit delicious friendfeed digg google myspace

Responses

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

    June 28th, 2010 at 23:14 (#)

    [...] This post was mentioned on Twitter by GoogleAPI Services, matteo. matteo said: Use Google Elevation Service API to detect a place’s elevation http://goo.gl/fb/sNEcK [...]

Leave a Response

CAPTCHA Image
CAPTCHA Audio
Refresh Image

Tag Cloud

360 actionscript 3 adobe blog css data database download easy email example excel facebook firefox flash flex Flex 4 free google html html5 image learn local mysql new online Panorama Personal PHP plugin register resize save server service spark teocomi test tutorial web website windows wordpress xls

WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.



©2010 Some Little News
Powered by teocomi using the Gridline Lite theme.