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用于添加必须在地图上显示的点 (lat/lng) 位置。
addVisible(address)StaticMap添加必须在地图上显示的地址位置。
beginPath()StaticMap开始一个新的路径定义。
clearMarkers()StaticMap清除当前的标记集。
clearPaths()StaticMap清除当前路径集。
clearVisibles()StaticMap清除当前的一组可见位置。
endPath()StaticMap完成以 beginPath() 开头的路径定义。
getAs(contentType)Blob将此对象中的数据作为转换为指定内容类型的 blob 返回。
getBlob()BlobBlob 形式获取图片数据。
getMapImage()Byte[]获取字节数组形式的原始图片数据。
getMapUrl()String获取地图图像的网址。
setCenter(latitude, longitude)StaticMap使用点 (lat/lng) 设置地图的中心。
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)

用于添加必须在地图上显示的点 (lat/lng) 位置。

// 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' 中的任何一个也 有效。对于 Google 文档,'text/markdown' 也有效。

返回

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)

使用点 (lat/lng) 设置地图的中心。

// 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 - 此地图实例,用于链接。

另请参阅