Class ElevationSampler

ElevationSampler

允許對特定位置的海拔高度取樣。
以下範例說明如何使用這個類別找出科羅拉多州丹佛到大街上的最高點、在地圖上繪製圖表,然後將地圖儲存至 Google 雲端硬碟。

// Get directions from Denver to Grand Junction.
var directions = Maps.newDirectionFinder()
    .setOrigin('Denver, CO')
    .setDestination('Grand Junction, CO')
    .setMode(Maps.DirectionFinder.Mode.DRIVING)
    .getDirections();
var route = directions.routes[0];

// Get elevation samples along the route.
var numberOfSamples = 30;
var response = Maps.newElevationSampler()
    .samplePath(route.overview_polyline.points, numberOfSamples)

// Determine highest point.
var maxElevation = Number.MIN_VALUE;
var highestPoint = null;
for (var i = 0; i < response.results.length; i++) {
  var sample = response.results[i];
  if (sample.elevation > maxElevation) {
    maxElevation = sample.elevation;
    highestPoint = sample.location;
  }
}

// Add the path and marker to a map.
var map = Maps.newStaticMap()
    .addPath(route.overview_polyline.points)
    .addMarker(highestPoint.lat, highestPoint.lng);

// Save the map to your drive
DocsList.createFile(Utilities.newBlob(map.getMapImage(), 'image/png', 'map.png'));

另請參閱

方法

方法傳回類型簡短說明
sampleLocation(latitude, longitude)Object傳回單一點 (經緯度) 的海拔高度資料。
sampleLocations(points)Object傳回一系列點 (經緯度) 的海拔高度資料。
sampleLocations(encodedPolyline)Object傳回編碼折線中點的海拔高度資料。
samplePath(points, numSamples)Object使用一系列點來定義一組樣本的海拔高度資料。
samplePath(encodedPolyline, numSamples)Object使用編碼折線定義,傳回一條線上多個樣本的海拔高度資料。

內容詳盡的說明文件

sampleLocation(latitude, longitude)

傳回單一點 (經緯度) 的海拔高度資料。

// Gets the elevation of Times Square using a point.
var data = Maps.newElevationSampler().sampleLocation(40.759011, -73.984472);
Logger.log(data.results[0].elevation);

參數

名稱類型說明
latitudeNumber要取樣的點緯度
longitudeNumber要取樣的點經度

回攻員

Object:包含海拔高度資料的 JSON 物件,如這裡所述


sampleLocations(points)

傳回一系列點 (lat/lng) 的海拔高度資料。

// Gets the elevation of Times Square and Central Park using points.
var data = Maps.newElevationSampler().sampleLocations([
    // Times Square
    40.759011, -73.984472,
    // Central Park
    40.777052, -73.975464
]);
Logger.log('Times Square: ' + data.results[0].elevation);
Logger.log('Central Park: ' + data.results[1].elevation);

參數

名稱類型說明
pointsNumber[]經緯度組合陣列

回攻員

Object:包含海拔高度資料的 JSON 物件,如這裡所述


sampleLocations(encodedPolyline)

傳回編碼折線中點的海拔高度資料。

// Gets the elevation of Times Square and Central Park using a polyline.
var data = Maps.newElevationSampler().sampleLocations('yvwwF|aqbMwoBiw@');
Logger.log('Times Square: ' + data.results[0].elevation);
Logger.log('Central Park: ' + data.results[1].elevation);

參數

名稱類型說明
encodedPolylineString要取樣的點編碼折線

回攻員

Object:包含海拔高度資料的 JSON 物件,如這裡所述


samplePath(points, numSamples)

使用一系列點來定義一組樣本的海拔高度資料。

// Gets the elevation of five points between Times Square and Central Park.
var data = Maps.newElevationSampler().samplePath([
    // Times Square
    40.759011, -73.984472,
    // Central Park
    40.777052, -73.975464
], 5);
for (var i = 0; i < data.results.length; i++) {
  Logger.log(data.results[i].elevation);
}

參數

名稱類型說明
pointsNumber[]一個經緯度對組陣列,會定義要取樣的路徑
numSamplesInteger沿著點路徑取樣的點數

回攻員

Object:包含海拔高度資料的 JSON 物件,如這裡所述


samplePath(encodedPolyline, numSamples)

使用編碼折線定義,傳回一條線上多個樣本的海拔高度資料。

// Gets the elevation of five points between Times Square and Central Park.
var data = Maps.newElevationSampler().samplePath('yvwwF|aqbMwoBiw@', 5);
for (var i = 0; i < data.results.length; i++) {
  Logger.log(data.results[i].elevation);
}

參數

名稱類型說明
encodedPolylineString這些點的編碼折線,定義指向的路徑
numSamplesInteger沿著點路徑取樣的點數

回攻員

Object:包含海拔高度資料的 JSON 物件,如這裡所述