得到googlemaps的高程'标记

时间:2016-04-22 12:08:15

标签: google-maps elevation

我尝试在地图上获得一个点的高程,但是googlemaps提出的高程函数' API不起作用,我不知道为什么。 似乎该程序甚至无法完成该功能。

这是我的功能:

var elevationService = new google.maps.ElevationService();

var requestElevation = {
'locations': gmarkers[0].getPosition()};

elevationService.getElevationForLocations(requestElevation, function(results, status) {
if (status == google.maps.ElevationStatus.OK) {
  if (results[0]) {
    document.getElementById('denivele_circuit').value = parseFloat(results[0].elevation.toFixed(1)) + "mètres";
  }
}  });

1 个答案:

答案 0 :(得分:1)

我的代码出现了javascript错误:Uncaught InvalidValueError: in property locations: not an Array

LocationElevationRequest中的locations属性必须是数组。

  

from the documentation google.maps.LocationElevationRequest对象规范

     

ElevationService发送的提升请求,包含要为其返回高程数据的离散坐标(LatLngs)列表。

     

<强>属性

     

位置类型:数组

     

要检索高程的离散位置。

var requestElevation = {
  'locations': [gmarkers[0].getPosition()]
};

proof of concept fiddle

代码段

var gmarkers = [];

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map
  });
  gmarkers.push(marker);
  var elevationService = new google.maps.ElevationService();

  var requestElevation = {
    'locations': [gmarkers[0].getPosition()]
  };

  elevationService.getElevationForLocations(requestElevation, function(results, status) {
    if (status == google.maps.ElevationStatus.OK) {
      if (results[0]) {
        document.getElementById('denivele_circuit').value = parseFloat(results[0].elevation.toFixed(1)) + " mètres";
      }
    }
  });

}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input id="denivele_circuit" />
<div id="map_canvas"></div>