允许创建和装饰静态地图图片。
以下示例展示了如何使用此类创建纽约市剧院区的地图(包括附近的火车站),并在一个简单的 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)
向当前路径定义中添加新地址。
// 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();
参数
名称 | 类型 | 说明 |
---|---|---|
address | String | 要添加的地址。 |
返回
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);
参数
名称 | 类型 | 说明 |
---|---|---|
latitude | Number | 新标记的纬度。 |
longitude | Number | 新标记的经度。 |
返回
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');
参数
名称 | 类型 | 说明 |
---|---|---|
address | String | 放置新标记的地址。 |
返回
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]);
参数
名称 | 类型 | 说明 |
---|---|---|
points | Number[] | 用于定义路径的纬度/经度对数组。 |
返回
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);
参数
名称 | 类型 | 说明 |
---|---|---|
polyline | String | 编码多段线。 |
返回
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();
参数
名称 | 类型 | 说明 |
---|---|---|
latitude | Number | 点的纬度。 |
longitude | Number | 点的经度。 |
返回
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)
参数
名称 | 类型 | 说明 |
---|---|---|
latitude | Number | 点的纬度。 |
longitude | Number | 点的经度。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。
另请参阅
addVisible(address)
添加必须在地图上可见的地址位置。
// Creates a map where New York and Boston are visible. var map = Maps.newStaticMap() .addVisible('New York, NY') .addVisible('Boston, MA');
参数
名称 | 类型 | 说明 |
---|---|---|
address | String | 必须在地图上显示的地址。 |
返回
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 网域可能暂时受到更严格的配额约束。
参数
名称 | 类型 | 说明 |
---|---|---|
contentType | String | 要转换为的 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)
使用点 (lat/lng) 设置地图中心。
// Creates a map centered on Times Square, using its coordinates. var map = Maps.newStaticMap().setCenter(40.759011, -73.984472);
参数
名称 | 类型 | 说明 |
---|---|---|
latitude | Number | 中心的纬度。 |
longitude | Number | 中心的经度。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。
另请参阅
setCenter(address)
使用地址设置地图中心。
// Creates a map centered on Times Square, using its address. var map = Maps.newStaticMap().setCenter('Times Square, New York, NY');
参数
名称 | 类型 | 说明 |
---|---|---|
address | String | 中心的地址。 |
返回
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);
参数
名称 | 类型 | 说明 |
---|---|---|
imageUrl | String | 指定用作标记的自定义图标的网址。图片可以是 PNG、JPEG 或 GIF 格式,但建议使用 PNG。 |
useShadow | Boolean | 表示标记应根据图片的可见区域及其不透明度/透明度生成阴影。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。
另请参阅
setFormat(format)
setLanguage(language)
设置要在地图上显示的文字(语言为其他语言)的语言。
// Creates a map with the language set to French. var map = Maps.newStaticMap().setLanguage('fr');
参数
名称 | 类型 | 说明 |
---|---|---|
language | String | BCP-47 语言标识符。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。
另请参阅
setMapType(mapType)
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');
参数
名称 | 类型 | 说明 |
---|---|---|
size | String | 来自 MarkerSize 的常量值。 |
color | String | 格式为“0xrrggbb”的字符串或来自 Color 的常量值。 |
label | String | 包含单个字符 A-Z 或 0-9 的字符串。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。
另请参阅
setMobile(useMobileTiles)
设置是否针对移动设备使用专用图块集。
// Creates a map that uses mobile-friendly tiles. var map = Maps.newStaticMap().setMobile(true);
参数
名称 | 类型 | 说明 |
---|---|---|
useMobileTiles | Boolean | 是否使用移动图块。 |
返回
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');
参数
名称 | 类型 | 说明 |
---|---|---|
weight | Integer | 线宽(以像素为单位)。 |
color | String | 线条颜色,格式为“0xrrggbb”的字符串或来自 Color 的常量值。 |
fillColor | String | 填充颜色,格式为“0xrrggbb”的字符串或来自 Color 的常量值。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。
另请参阅
setSize(width, height)
设置地图图片的宽度和高度(以像素为单位)。
// Creates a map 400px wide by 300px high. var map = Maps.newStaticMap().setSize(400, 300);
参数
名称 | 类型 | 说明 |
---|---|---|
width | Integer | 图片的宽度(以像素为单位)。 |
height | Integer | 图片的高度,以像素为单位。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。
另请参阅
setZoom(zoom)
用于设置地图使用的缩放比例或放大级别。
// Creates a map with a zoom factor of 10. var map = Maps.newStaticMap().setZoom(10);
参数
名称 | 类型 | 说明 |
---|---|---|
zoom | Integer | 值介于 0 和 21 之间(含 0 和 21)。 |
返回
StaticMap
- 此地图实例,用于实现链式连接。