2016-04-22 103 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

由於它的工作原理。我以爲我已經嘗試過,但我可能沒有。 –