javascript - 谷歌地球根据纬度和经度值计算海拔高度

标签 javascript dictionary elevation

以下网址提供了一个通过单击 map 获取海拔高度的绝佳示例。

https://developers.google.com/maps/documentation/javascript/examples/elevation-simple

我想输入特定的纬度和经度值而不是单击。

使用以下 HTML 和 JavaScript 会导致输入经纬度时出现“未定义”。我错过了什么?

<!DOCTYPE html>
<html>
<body>
<h1>Enter Lat-Long to get Google Earth Level</h1>

<p>Lat:</p>
<input id="lat" type="number">

<p>Long:</p>
<input id="long" type="number">
<button type="button" onclick="myFunction()">Submit</button>

<p id="level"></p>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

<script>

function myFunction() {
    var elevator = new google.maps.ElevationService();
    var denali = new google.maps.LatLng(document.getElementById('lat').value , document.getElementById('long').value);
    var positionalRequest = {'locations':[denali]};

    var text;

    elevator.getElevationForLocations(positionalRequest, function (results, status) {
        if (status == google.maps.ElevationStatus.OK) {

            // Retrieve the first result
            if (results[0]) {
                text = results[0].elevation;
            } else {
                alert('No results found');
            }
        }
        else {
            alert('Elevation service failed due to: ' + status);
        }
    });

    document.getElementById("level").innerHTML = text;
}


</script>

</body>
</html>

最佳答案

这是一个带有硬编码位置的示例。

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
  function getLocations() {
    var elevator = new google.maps.ElevationService();

    // places in Brussels (a city built on hills, so there are altitude differences)
    var places = [
      {name: "Altitude 100", lat: 50.81665455596093, lng: 4.336802423000336},
      {name: "Grand Place", lat: 50.84676859190515, lng: 4.352380692958832},
      {name: "Atomium", lat: 50.8949350060238, lng: 4.341544568538666}
    ];
    var locations = [];
    for (var i=0; i<places.length; i++) {
      locations.push( new google.maps.LatLng(places[i].lat, places[i].lng) );
    }
    var positionalRequest = {
      'locations': locations
    }
    // Initiate the elevation request
    elevator.getElevationForLocations(positionalRequest, function(results, status) {
      if (status == google.maps.ElevationStatus.OK) {
        if (results[0]) {
          for (var i=0; i< results.length; i++) {
            document.getElementById('log').innerHTML +=
              '"' + places[i].name +'", elevation: ' + results[i].elevation.toFixed(2) + 'm<br>';
          }
        }
      }
    });
  }
  google.maps.event.addDomListener(window, 'load', getLocations);
</script>
<div id="log"></div>
<小时/>

这是一个包含输入元素的示例。

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
  function getLocations(locations) {
  var elevator = new google.maps.ElevationService();
    var positionalRequest = {
      'locations': locations
    }
    // Initiate the elevation request
    elevator.getElevationForLocations(positionalRequest, function(results, status) {
      if (status == google.maps.ElevationStatus.OK) {
        if (results[0]) {
          for (var i=0; i< results.length; i++) {
            document.getElementById('log').innerHTML =
              'elevation: ' + results[i].elevation.toFixed(2) + 'm<br>';
          }
        }
      }
    });
  }
  function myFunction() {
    var locations = [
      new google.maps.LatLng(
        Number(document.getElementById('lat').value),
        Number(document.getElementById('lng').value)
      )
    ];
    getLocations(locations);
  }
</script>
<div id="log"></div>

<p>Lat:</p>
<input id="lat" type="number">
<p>Long:</p>
<input id="lng" type="number">
<button type="button" onclick="myFunction()">Submit</button>

关于javascript - 谷歌地球根据纬度和经度值计算海拔高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27898720/

相关文章:

javascript - 正则表达式字符串以在 Javascript 中不起作用为结尾

python - 替换 Python 字典中的值

python - 如何在numpy数组中应用带有数组作为值的字典

ios - MKMapView,如何计算以米为单位的高程?

python - Windows错误 : [Error 740] The requested operation requires elevation even after disabling UAC

javascript - 如何将此类代码转换为钩子(Hook)? - 原生 react

javascript - 使 html 图像区域 ‘coord’ 闪烁或发光...

javascript - 位置 :absolute element not positioned correctly

json - 从 R 中的字典列表中提取数据

mapbox - 如何从 Mapbox 获取高程剖面数据?