Class StaticMap

StaticMap

允许创建和装饰静态地图图片。

以下示例展示了如何使用此类创建纽约市剧院区(包括附近的火车站)的地图,并将其显示在一个简单的 Web 应用中。

// Create a map centered on Times Square.
var map = Maps.newStaticMap()
    .setSize(600, 600)
    .setCenter('Times Square, New York, NY');

// Add markers for the nearbye train stations.
map.setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.RED, 'T');
map.addMarker('Grand Central Station, New York, NY');
map.addMarker('Penn Station, New York, NY');

// Show the boundaries of the Theatre District.
var corners = [
  '8th Ave & 53rd St, New York, NY',
  '6th Ave & 53rd St, New York, NY',
  '6th Ave & 40th St, New York, NY',
  '8th Ave & 40th St, New York, NY'
];
map.setPathStyle(4, Maps.StaticMap.Color.BLACK, Maps.StaticMap.Color.BLUE);
map.beginPath();
for (var i = 0; i < corners.length; i++) {
  map.addAddress(corners[i]);
}
// All static map URLs require an API key.
var url = map.getMapUrl() + "&key=YOUR_API_KEY";

另请参阅

方法

方法返回类型简介
addAddress(address)StaticMap向当前路径定义添加新地址。
addMarker(latitude, longitude)StaticMap使用点 (lat/lng) 向地图添加标记。
addMarker(address)StaticMap使用地址向地图添加标记。
addPath(points)StaticMap使用点数组向地图添加路径。
addPath(polyline)StaticMap使用编码多段线向地图添加路径。
addPoint(latitude, longitude)StaticMap向当前路径定义添加新点 (lat/lng)。
addVisible(latitude, longitude)StaticMap用于添加必须在地图中显示的点(纬度/经度)位置。
addVisible(address)StaticMap用于添加必须显示在地图上的地址位置。
beginPath()StaticMap开始新的路径定义。
clearMarkers()StaticMap清除当前的标记集。
clearPaths()StaticMap清除当前的路径集。
clearVisibles()StaticMap清除当前的可见位置集。
endPath()StaticMap完成以 beginPath() 开头的路径定义。
getAs(contentType)Blob返回此对象内的数据,并将其作为 blob 转换为指定内容类型。
getBlob()Blob获取 Blob 形式的图片数据。
getMapImage()Byte[]获取字节数组形式的原始图片数据。
getMapUrl()String获取地图图像的网址。
setCenter(latitude, longitude)StaticMap使用点(纬度/经度)设置地图的中心。
setCenter(address)StaticMap使用地址设置地图的中心。
setCustomMarkerStyle(imageUrl, useShadow)StaticMap用于设置创建新标记时使用的自定义标记图像。
setFormat(format)StaticMap设置地图图像的格式。
setLanguage(language)StaticMap设置地图上文本使用的语言(如果有)。
setMapType(mapType)StaticMap用于设置要显示的地图类型。
setMarkerStyle(size, color, label)StaticMap设置创建新标记时要使用的标记样式。
setMobile(useMobileTiles)StaticMap设置是否针对移动设备使用专用图块集。
setPathStyle(weight, color, fillColor)StaticMap设置创建新路径时要使用的路径样式。
setSize(width, height)StaticMap设置地图图像的宽度和高度(以像素为单位)。
setZoom(zoom)StaticMap设置地图的缩放比例或放大级别。

详细文档

addAddress(address)

向当前路径定义添加新地址。

// Creates a map and adds a path from New York to Boston.
var map = Maps.newStaticMap()
    .beginPath()
    .addAddress('New York, NY')
    .addAddress('Boston, MA')
    .endPath();

参数

名称类型说明
addressString要添加的地址。

弃踢回攻

StaticMap - 此地图实例,用于链接。


addMarker(latitude, longitude)

使用点 (lat/lng) 向地图添加标记。

// Creates a map and adds a marker at the specified coordinates.
var map = Maps.newStaticMap().addMarker(40.741799, -74.004207);

参数

名称类型说明
latitudeNumber新标记的纬度。
longitudeNumber新标记的经度。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


addMarker(address)

使用地址向地图添加标记。

// Creates a map and adds a marker at the specified address.
var map = Maps.newStaticMap().addMarker('76 9th Ave, New York NY');

参数

名称类型说明
addressString用于放置新标记的地址。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


addPath(points)

使用点数组向地图添加路径。

// Creates a map and adds a path from New York to Boston.
var map = Maps.newStaticMap()
    .addPath([40.714353, -74.005973, 42.358431, -71.059773]);

参数

名称类型说明
pointsNumber[]用于定义路径的纬度/经度对数组。

弃踢回攻

StaticMap - 此地图实例,用于链接。


addPath(polyline)

使用编码多段线向地图添加路径。

// Creates a map and adds a path from New York to Boston.
var polyline = Maps.encodePolyline([40.714353, -74.005973, 42.358431, -71.059773]);
var map = Maps.newStaticMap().addPath(polyline);

参数

名称类型说明
polylineString编码多段线。

弃踢回攻

StaticMap - 此地图实例,用于链接。


addPoint(latitude, longitude)

向当前路径定义添加新点 (lat/lng)。

// Creates a map and adds a path from New York to Boston.
var map = Maps.newStaticMap()
    .beginPath()
    .addPoint(40.714353, -74.005973)
    .addPoint(42.358431, -71.059773)
    .endPath();

参数

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

弃踢回攻

StaticMap - 此地图实例,用于链接。


addVisible(latitude, longitude)

添加必须在地图上显示的点(纬度/经度)位置。

// Creates a map where New York and Boston are visible.
var map = Maps.newStaticMap()
    .addVisible(40.714353, -74.005973);
    .addVisible(42.358431, -71.059773)

参数

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

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


addVisible(address)

添加必须在地图上显示的地址位置。

// Creates a map where New York and Boston are visible.
var map = Maps.newStaticMap()
    .addVisible('New York, NY')
    .addVisible('Boston, MA');

参数

名称类型说明
addressString一个必须显示在地图中的地址。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


beginPath()

开始新的路径定义。调用 addAddress()addPoint() 可定义路径中的每个新顶点。调用 endPath() 后,即完成了路径。

// Creates a map and adds a path from New York to Boston.
var map = Maps.newStaticMap()
    .beginPath()
    .addAddress('New York, NY')
    .addAddress('Boston, MA')
    .endPath();

弃踢回攻

StaticMap - 此地图实例,用于链接。


clearMarkers()

清除当前的标记集。

var map = Maps.newStaticMap();
// ...
// Do something interesting here ...
// ...
// Remove all markers on the map.
map.clearMarkers();

弃踢回攻

StaticMap - 此地图实例,用于链接。


clearPaths()

清除当前的一组路径。

var map = Maps.newStaticMap();
// ...
// Do something interesting here ...
// ...
// Remove all paths on the map.
map.clearPaths();

弃踢回攻

StaticMap - 此地图实例,用于链接。


clearVisibles()

清除当前的可见位置集。

var map = Maps.newStaticMap();
// ...
// Do something interesting here ...
// ...
// Remove all visible locations created with addVisible().
map.clearVisibles();

弃踢回攻

StaticMap - 此地图实例,用于链接。


endPath()

完成以 beginPath() 开头的路径定义。

// Creates a map and adds a path from New York to Boston.
var map = Maps.newStaticMap()
    .beginPath()
    .addAddress('New York, NY')
    .addAddress('Boston, MA')
    .endPath();

弃踢回攻

StaticMap - 此地图实例,用于链接。


getAs(contentType)

返回此对象内的数据,并将其作为 blob 转换为指定内容类型。此方法会为文件名添加适当的扩展名,例如“myfile.pdf”。不过,它假设最后一个英文句点后面的文件名部分(如果有)是应该替换的现有扩展名。因此,“ShoppingList.12.25.2014”将变为“ShoppingList.12.25.pdf”。

如需查看转化次数的每日配额,请参阅 Google 服务的配额。新创建的 Google Workspace 网域可能会暂时受到更严格的配额限制。

参数

名称类型说明
contentTypeString要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一有效的选项。对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp''image/gif''image/jpeg''image/png' 中的任何一个也都有效。

弃踢回攻

Blob - 作为 blob 的数据。


getBlob()

获取 Blob 形式的图片数据。

// Creates a map centered on Times Square and saves it to Google Drive.
var map = Maps.newStaticMap().setCenter('Times Square, New York, NY');
DocsList.createFile(map);  // You can call map.getBlob() explicitly or use it
                           // implicitly by passing the map where a blob is expected.

弃踢回攻

Blob - 采用所选图片格式的地图图片。


getMapImage()

获取字节数组形式的原始图片数据。

一般来说,最好使用 getBlob(),这样就可以更轻松地与其他服务进行交互。

// Creates a map centered on Times Square and saves it to Google Drive.
var map = Maps.newStaticMap().setCenter('Times Square, New York, NY');
DocsList.createFile(Utilities.newBlob(map.getMapImage(), 'image/png', 'map.png'));

弃踢回攻

Byte[] - 采用所选图片格式的地图图片。


getMapUrl()

获取地图图片的网址。

// Creates a map centered on Times Square and gets the URL.
var map = Maps.newStaticMap().setCenter('Times Square, New York, NY');
// All static map URLs require an API key.
Logger.log(map.getMapUrl() + "&key=YOUR_API_KEY");

弃踢回攻

String - 网址:地图图片网址。


setCenter(latitude, longitude)

使用点(纬度/经度)设置地图的中心。

// Creates a map centered on Times Square, using its coordinates.
var map = Maps.newStaticMap().setCenter(40.759011, -73.984472);

参数

名称类型说明
latitudeNumber中心的纬度。
longitudeNumber中心的经度。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setCenter(address)

使用地址设置地图的中心。

// Creates a map centered on Times Square, using its address.
var map = Maps.newStaticMap().setCenter('Times Square, New York, NY');

参数

名称类型说明
addressString中心的地址。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setCustomMarkerStyle(imageUrl, useShadow)

用于设置创建新标记时使用的自定义标记图像。已添加的标记不受影响。

// Creates a map with markers set to be medium sized, black, and labeled with the number "1".
var map = Maps.newStaticMap()
    .setCustomMarkerStyle('http://www.example.com/marker.png', false);

参数

名称类型说明
imageUrlString指定用作标记的自定义图标的网址。图片可以是 PNG、JPEG 或 GIF 格式,但建议采用 PNG。
useShadowBoolean表示标记应根据图片的可见区域及其不透明度/透明度生成阴影。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setFormat(format)

设置地图图像的格式。

// Creates a map with the image format set to PNG.
var map = Maps.newStaticMap().setFormat(Maps.StaticMap.Format.PNG);

参数

名称类型说明
formatString来自 Format 的常量值。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setLanguage(language)

设置地图上文本使用的语言(如果有)。

// Creates a map with the language set to French.
var map = Maps.newStaticMap().setLanguage('fr');

参数

名称类型说明
languageStringBCP-47 语言标识符。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setMapType(mapType)

设置要显示的地图类型。

// Creates a satellite map.
var map = Maps.newStaticMap().setMapType(Maps.StaticMap.Type.SATELLITE);

参数

名称类型说明
mapTypeString来自 Type 的常量值。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setMarkerStyle(size, color, label)

设置创建新标记时要使用的标记样式。已添加的标记不受影响。

// Creates a map with markers set to be medium sized, black, and labeled with the number "1".
var map = Maps.newStaticMap()
    .setMarkerStyle(Maps.StaticMap.MarkerSize.MID, Maps.StaticMap.Color.BLACK , '1');

参数

名称类型说明
sizeString来自 MarkerSize 的常量值。
colorString格式为“0xrrggbb”的字符串或 Color 中的常量值。
labelString包含单个字符 A-Z 或 0-9 的字符串。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setMobile(useMobileTiles)

设置是否针对移动设备使用专用图块集。

// Creates a map that uses mobile-friendly tiles.
var map = Maps.newStaticMap().setMobile(true);

参数

名称类型说明
useMobileTilesBoolean是否使用移动图块。

弃踢回攻

StaticMap - 此地图实例,用于链接。


setPathStyle(weight, color, fillColor)

设置创建新路径时要使用的路径样式。已添加的路径不会受到影响。

// Creates a map with paths set to be 1 pixel wide with a black line and a white fill.
var map = Maps.newStaticMap()
    .setPathStyle(1, Maps.StaticMap.Color.BLACK , 'red');

参数

名称类型说明
weightInteger线宽(以像素为单位)。
colorString线条颜色,表示为字符串(格式为“0xrrggbb”)或常量值(来自 Color)。
fillColorString填充颜色,格式为“0xrrggbb”的字符串或 Color 中的常量值。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setSize(width, height)

设置地图图片的宽度和高度(以像素为单位)。

// Creates a map 400px wide by 300px high.
var map = Maps.newStaticMap().setSize(400, 300);

参数

名称类型说明
widthInteger图片的宽度(以像素为单位)。
heightInteger图片的高度(以像素为单位)。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅


setZoom(zoom)

设置地图使用的缩放比例或放大级别。

// Creates a map with a zoom factor of 10.
var map = Maps.newStaticMap().setZoom(10);

参数

名称类型说明
zoomInteger介于 0 和 21 之间(包括 0 和 21)的值。

弃踢回攻

StaticMap - 此地图实例,用于链接。

另请参阅