2016-04-22 101 views
-1

我尝试获取地图上某个点的高程,但googlemaps的API提出的海拔函数不起作用,我不知道为什么。 看来,程序甚至没有通过该功能。获得Google地图标记的海拔

这里我的功能:

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

我得到一个JavaScript错误与您的代码:Uncaught InvalidValueError: in property locations: not an Array

LocationElevationRequestlocations属性必须是一个数组。

from the documentationgoogle.maps.LocationElevationRequest对象规范

由含有离散坐标(LatLngs)为其返回高程数据的列表中的ElevationService发送的海拔请求。

属性

位置类型:阵列

要检索海拔的离散位置。

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>

+0

由于它的工作原理。我以为我已经尝试过,但我可能没有。 –