【问题标题】:Getting the Altitude / Elevation from Google Maps address with the Elevation API使用 Elevation API 从 Google 地图地址获取海拔/海拔
【发布时间】:2018-02-04 14:22:43
【问题描述】:

我正在尝试在 PHP 中回显地址的提升。 我正在使用 Google Elevation API - LINK

我已经有了纬度和经度。 以下链接:

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

将在 json 中生成这些结果:

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

如何回显海拔? 类似<?php echo 'elevation'; ?>

谢谢

【问题讨论】:

  • 嘿,您共享了您的私有 API_KEY。我建议您将其从问题中删除并从 API 控制台为您重新生成一个新的

标签: json google-maps google-elevation-api


【解决方案1】:

您可以通过将响应更改为 xml 格式来做到这一点,以使事情变得更容易:

https://maps.googleapis.com/maps/api/elevation/xml?locations=40.7143528,-74.0059731&key=yourKey 如果您的 php 代码支持它,您可以从那里执行此操作:

<?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;
?>

【讨论】:

  • 谢谢。我得到2个错误。我可能会用function file_get_contents_curl( $url ) { $ch = curl_init(); curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE ); curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE ); $data = curl_exec( $ch ); curl_close( $ch ); return $data; } 修复并更改为file_get_contents_curl [link]gist.github.com/schalkjoubert/0d9be4969990e37dda6594347e8ce983
  • 问题似乎是由于证书验证错误而无法连接。 This is not needed and you can disable it.
  • 谢谢,试过了,走得更远了。我确实承认,我确实承认,我正在超越我的水平。 $url = "https://maps.googleapis.com/maps/api/elevation/json?locations=40.7143528,-74.0059731&amp;key=AIzaSyCM1fCTYKPhk6tDCqsVNhcXto6aqjUc0yY"; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL,$url); $result=curl_exec($ch); curl_close($ch); print json_encode($result, JSON_PRETTY_PRINT); 这会打印所有内容,包括海拔高度。
  • 这是我在屏幕上看到的:"{\n \"results\" : [\n {\n \"elevation\" : 9.774918556213379,\n \"location\" : {\n \"lat\" : 40.7143528,\n \"lng\" : -74.00597310000001\n },\n \"resolution\" : 19.08790397644043\n }\n ],\n \"status\" : \"OK\"\n}\n"
  • 那是因为你的代码末尾有print json_encode($result, JSON_PRETTY_PRINT);。只需删除该行,它就会停止打印到屏幕上。
【解决方案2】:

线程很旧,但我找到了一个替代方案,可以用来在没有谷歌和没有密钥的情况下进行海拔查询。 链接: https://api.opentopodata.org/v1/eudem25m?locations=51.875127,10.65432 给出以下结果:

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

可以用php求值:

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

注意:在循环中查询时,必须包含睡眠,例如睡眠(2);

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多