Class ElevationSampler

ElevationSampler

允许对特定位置的海拔进行抽样。
以下示例展示了如何使用此类来确定从科罗拉多州丹佛到大章克申的路线上的最高点,将其绘制在地图上,并将地图保存到 Google 云端硬盘。

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

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

// Determine highest point.

let highestLocation = null;
let highestElevation = Number.MIN_VALUE;
for (const sample of response.results) {
  if (sample.elevation > highestElevation) {
    highestElevation = sample.elevation;
    highestLocation = sample.location;
  }
}

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

// Save the map to your drive
DriveApp.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.
const data = Maps.newElevationSampler().sampleLocation(40.759011, -73.984472);
Logger.log(data.results[0].elevation);

参数

名称类型说明
latitudeNumber要抽样的点的纬度。
longitudeNumber要抽样的点的经度。

返回

Object - 包含海拔数据的 JSON 对象,如此处所述。


sampleLocations(points)

返回一系列点(纬度/经度)的海拔数据。

// Gets the elevation of Times Square and Central Park using points.
const 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.
const 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.
const data = Maps.newElevationSampler().samplePath(
    [
      // Times Square
      40.759011,
      -73.984472,
      // Central Park
      40.777052,
      -73.975464,
    ],
    5,
);
for (let 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.
const data = Maps.newElevationSampler().samplePath('yvwwF|aqbMwoBiw@', 5);
for (let i = 0; i < data.results.length; i++) {
  Logger.log(data.results[i].elevation);
}

参数

名称类型说明
encodedPolylineString一个编码的多段线,包含定义抽样路径的点。
numSamplesInteger沿点路径采样的点数。

返回

Object - 包含海拔数据的 JSON 对象,如此处所述。