Schalk Joubert
Schalk Joubert

Reputation: 398

Getting the Altitude / Elevation from Google Maps address with the Elevation API

Im trying the echo the elevation of an address in PHP. I am using the Google Elevation API - LINK

I already have the latitude and longitude. The following link:

https://maps.googleapis.com/maps/api/elevation/json?locations=40.7143528,-74.0059731&key=myapikey

will produce these results in json:

{ "results" : [ { "elevation" : 9.774918556213379, "location" : { "lat" : 40.7143528, "lng" : -74.00597310000001 }, "resolution" : 19.08790397644043 } ], "status" : "OK" }

How can I echo the elevation? something like <?php echo 'elevation'; ?>

Thank you

Upvotes: 2

Views: 4415

Answers (2)

guenter47
guenter47

Reputation: 594

The thread is old but I found an alternative that can be used to do an elevation query without google and without a key. The link: https://api.opentopodata.org/v1/eudem25m?locations=51.875127,10.65432 gives the following result:

{
  "results": [
    {
      "dataset": "eudem25m", 
      "elevation": 352.5389709472656, 
      "location": {
        "lat": 51.875127, 
        "lng": 10.65432
      }
    }
  ], 
  "status": "OK"
}

which can be evaluated with php:

$json = json_decode($result, true);
$result = $json['results'][0]['elevation'];

Note: when querying in a loop, a sleep must be included, e.g. sleep(2);

Upvotes: 1

Franco Vizcarra
Franco Vizcarra

Reputation: 426

You can do this by changing the response to be xml format to make things easier:

https://maps.googleapis.com/maps/api/elevation/xml?locations=40.7143528,-74.0059731&key=yourKey From there if your php code supports it you can do this:

<?php
$response = file_get_contents('https://maps.googleapis.com/maps/api/elevation/xml?locations=40.7143528,-74.0059731&key=myapikey');
$results = new SimpleXMLElement($response);
echo $results->elevation;
?>

Upvotes: 5

Related Questions