Class StaticMap

StaticMap

可用來建立和修飾靜態地圖圖片。

以下範例說明如何使用這個類別建立紐約市劇場 (包括鄰近的火車站) 的地圖,並顯示在簡單的網頁應用程式中。

// 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使用點 (經緯度) 在地圖上新增標記。
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完成以 startPath() 啟動的路徑定義。
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設定地圖上文字要使用的語言 (「avaialbe」)。
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)

使用點 (經緯度) 在地圖上新增標記。

// 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()

完成以 startPath() 啟動的路徑定義。

// 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);

參數

名稱類型說明
formatStringFormat 中的常數值。

回攻員

StaticMap — 此地圖例項,用於鏈結。

另請參閱


setLanguage(language)

設定地圖上文字要使用的語言 (「Avaialbe」)。

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

參數

名稱類型說明
languageStringBCP-47 語言 ID。

回攻員

StaticMap — 此地圖例項,用於鏈結。

另請參閱


setMapType(mapType)

設定要顯示的地圖類型。

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

參數

名稱類型說明
mapTypeStringType 中的常數值。

回攻員

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');

參數

名稱類型說明
sizeStringMarkerSize 中的常數值。
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 (含首尾) 的值。

回攻員

StaticMap — 此地圖例項,用於鏈結。

另請參閱