本記事では、Google Apps ScriptでGoogleマップに対して使うことができるMapsという処理の一覧表を用意しました。Googleの公式ドキュメントは英語なので、分かりづらいところもありますので、みなさんがそこで挫折しないように日本語で内容が把握できるようにします。
まだすべてのメソッドで日本語での使い方が解説できているわけではありませんが、最終的にはすべての使い方を日本語で解説するページを用意していきます。
日本語化されている記事の見分け方は、簡単な説明列で日本語が記載されているものについては日本語記事が用意できています。メソッド名をクリックすることで対象の記事を閲覧することができます。
日本語の記事が出来上がっていないメソッドについてはGoogleから提供されている公式ドキュメント(英語)へのリンクとなっています。
Mapsの概要
Mapsとは、Google Apps ScriptでGoogleマップに対する操作を行うコードですが、Mapsに用意されているメソッドでは、次のような操作を行うことができます。
- 道順検索
- 座標の検索
- マップの静止画生成
こちらのコードで実行できるものは基本的に静止画のマップを使った描画となります。一般的なウェブサイトなどで表示されているインタラクティブに操作ができるGoogleマップはGoogle Maps APIというもので作成されているもので、今回ご紹介しているGASで利用できるMapsとは別物になります。
クラス名一覧
| DirectionFinder | DirectionFinderEnums | ElevationSampler |
| Geocoder | StaticMap | StaticMapEnums |
※クリックすると対象場所にジャンプします
列挙型一覧
| Avoid | Color | Format |
| MarkerSize | Mode | Type |
※クリックすると対象場所にジャンプします
クラス別メソッド名一覧
Class DirectionFinder
2点間の道順を検索する際に利用するのがこのDirectionFinderクラスです。下記サンプルコードでは、新宿駅から日暮里駅を経由して成田空港まで向かうという設定でDirectionFinderクラスをどのように利用するかということを簡単に説明します。→ こちら ←のをクリックすれば、サンプルコードにアクセスしてコードの実行結果をすぐに確認ができます。コードを実行すると自分のメールアドレス宛に画像が添付されて送られてきますので、メールの内容をご確認下さい。
それぞれのメソッドに対する詳細な解説については、各メソッドのページにて確認して下さい。
//DirectionFinderクラスの使用例
function sampleCodeForDirectionFinder() {
//道順を設定します
let directions = Maps.newDirectionFinder()
.setOrigin('新宿駅')
.addWaypoint('日暮里駅')
.setDestination('成田空港駅')
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
let route = directions.routes[0];
//マーカーのスタイルを設定します
let markerSize = Maps.StaticMap.MarkerSize.MID;
let markerColor = Maps.StaticMap.Color.GREEN
let markerLetterCode = 'A'.charCodeAt();
//マップにマーカーをセットします
let map = Maps.newStaticMap();
for (let i = 0; i < route.legs.length; i++) {
let leg = route.legs[i];
if (i == 0) {
//スタート地点にマーカーをセットします
map.setMarkerStyle(markerSize, markerColor, String.fromCharCode(markerLetterCode));
map.addMarker(leg.start_location.lat, leg.start_location.lng);
markerLetterCode++;
}
//ゴール地点にマーカーをセットします
map.setMarkerStyle(markerSize, markerColor, String.fromCharCode(markerLetterCode));
map.addMarker(leg.end_location.lat, leg.end_location.lng);
markerLetterCode++;
}
//道順をハイライトします
map.addPath(route.overview_polyline.points);
//自分宛てにメールで画像を添付します。
let toAddress = Session.getActiveUser().getEmail();
let options = {
attachments: [Utilities.newBlob(map.getMapImage(), 'image/png')
.setName("道順サンプル")],
}
MailApp.sendEmail(
toAddress,
"新宿駅から日暮里駅経由の成田空港駅までの道順",
"添付ファイルをご確認下さい。",
options
);
}
| メソッド名 | 返り値 | 簡単な説明 |
| addWaypoint | DirectionFinder | Adds a waypoint that the route must pass through, using a point (lat/lng). |
| clearWaypoints | DirectionFinder | Clears the current set of waypoints. |
| getDirections | Object | Gets the directions using the origin, destination, and other options that were set. |
| setAlternatives | DirectionFinder | Sets whether or not alternative routes should be returned, instead of just the highest ranked route (defaults to false). |
| setArrive | DirectionFinder | Sets the desired time of arrival (when applicable). |
| setAvoid | DirectionFinder | Sets whether to avoid certain types of restrictions. |
| setDepart | DirectionFinder | Sets the desired time of departure (when applicable). |
| setDestination | DirectionFinder | Sets the ending location for which to calculate directions to, using a point (lat/lng). |
| setLanguage | DirectionFinder | Sets the language to be used for the directions. |
| setMode | DirectionFinder | Sets the mode of travel (defaults to driving). |
| setOptimizeWaypoints | DirectionFinder | Sets whether or not to optimize the provided route by rearranging the waypoints in a more efficient order (defaults to false). |
| setOrigin | DirectionFinder | Sets the starting location from which to calculate directions, using a point (lat/lng). |
| setRegion | DirectionFinder | Sets a region to use when interpreting location names. |
Class DirectionFinderEnums
DirectionFinderで利用できる列挙型
| プロパティ | タイプ | 簡単な説明 |
| Avoid | Avoid | 検索に制限をかける |
| Mode | Mode | 検索条件を設定する |
Class ElevationSampler
ElevationSamplerクラスでは、指定した場所の高度を取得できます。下記サンプルコードでは、新宿駅から成田駅の経路に沿って、一番高度の高い場所にマーカーをセットしたマップ画像を自分のメールアドレス宛にファイル添付して送信するコードとなります。ElevationSamplerクラスの大まかな使い方がサンプルの中で理解できると思います。すぐに実行可能なサンプルは→ こちら ←をクリック
//ElevationSamplerクラスの使用例
function sampleCodeForElevationSampler() {
//新宿駅から成田空港駅までの道順を設定します
var directions = Maps.newDirectionFinder()
.setOrigin('新宿駅')
.setDestination('成田空港駅')
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
var route = directions.routes[0];
//道順に沿って高度を取得します
var numberOfSamples = 30;
var response = Maps.newElevationSampler()
.samplePath(route.overview_polyline.points, numberOfSamples)
//最高度地点を算出します
var maxElevation = Number.MIN_VALUE;
var highestPoint = null;
for (var i = 0; i < response.results.length; i++) {
var sample = response.results[i];
if (sample.elevation > maxElevation) {
maxElevation = sample.elevation;
highestPoint = sample.location;
}
}
//道順をハイライトして最高度地点にマーカーをセットします
var map = Maps.newStaticMap()
.addPath(route.overview_polyline.points)
.addMarker(highestPoint.lat, highestPoint.lng);
//自分宛てにメールで画像を添付します。
let toAddress = Session.getActiveUser().getEmail();
let options = {
attachments: [Utilities.newBlob(map.getMapImage(), 'image/png')
.setName("最高度地点")],
}
MailApp.sendEmail(
toAddress,
"新宿駅から成田空港駅間での最高度地点",
"添付ファイルをご確認下さい。",
options
);
}
| メソッド名 | 返り値 | 簡単な説明 |
| sampleLocation | Object | Returns elevation data for a single point (lat/lng). |
| sampleLocations | Object | Returns elevation data for a series of points (lat/lng). |
| samplePath | Object | Returns elevation data for a number of samples along a line, defined using a series of points. |
Class Geocoder
Geocoderクラスでは、文字として入力された住所情報と座標を変換することができます。下記サンプルコードでは、『新宿駅』というキーワードで座標を取得しています。Geocoderクラスの大まかな使い方がサンプルの中で理解できると思います。すぐに実行可能なサンプルは→ こちら ←をクリック
//Geocoderクラスの使用例
function sampleCodeForGeocoder() {
// Find the best matches for "Main St" in Colorado.
var response = Maps.newGeocoder()
// The latitudes and longitudes of southwest and northeast corners of Colorado, respectively.
.setBounds(35.687267, 139.696014, 35.693575, 139.705500)
.geocode("新宿駅");
var map = Maps.newStaticMap();
//新宿駅の座標を取得してログに出力する
for (var i = 0; i < response.results.length && i < 9; i++) {
var result = response.results[i];
var lat = result.geometry.location.lat;
var lng = result.geometry.location.lng;
console.log("経度:" + lat);
console.log("緯度:" + lng);
console.log("Googleマップで検索して下さい\n" + lat + ", " + lng);
}
}
| メソッド名 | 返り値 | 簡単な説明 |
| geocode | Object | Gets the approximate geographic points for a given address. |
| reverseGeocode | Object | Gets the approximate addresses for a given geographic point. |
| setBounds | Geocoder | Sets the bounds of an area that should be given extra preference in the results. |
| setLanguage | Geocoder | Sets the language to be used in the results. |
| setRegion | Geocoder | Sets a region to use when interpreting location names. |
Class StaticMap
StaticMapクラスでは、マップの静止画を作成したりマップを装飾することができます。下記サンプルコードでは、静止画に境界範囲を設定してメールを送信する処理となっています。このサンプルからStaticCodeクラスの使い方を学ベルかと思います。すぐに実行できるサンプルコードは→ ここ ←をクリックするとスクリプトエディタへアクセスできます。
| メソッド名 | 返り値 | 簡単な説明 |
| addAddress | StaticMap | Adds a new address to the current path definition. |
| addMarker | StaticMap | Adds a marker to the map using a point (lat/lng). |
| addPath | StaticMap | Adds a path to the map using an array of points. |
| addPoint | StaticMap | Adds a new point (lat/lng) to the current path definition. |
| addVisible | StaticMap | Adds a point (lat/lng) location that must be visible in the map. |
| beginPath | StaticMap | Starts a new path definition. |
| clearMarkers | StaticMap | Clears the current set of markers. |
| clearPaths | StaticMap | Clear the current set of paths. |
| clearVisibles | StaticMap | Clears the current set of visible locations. |
| endPath | StaticMap | Completes a path definition started with beginPath(). |
| getAs | Blob | Return the data inside this object as a blob converted to the specified content type. |
| getBlob | Blob | Gets the image data as a Blob. |
| getMapImage | Byte[] | Gets the raw image data as a byte array. |
| getMapUrl | String | Gets the URL of the map image. |
| setCenter | StaticMap | Sets the center of the map using a point (lat/lng). |
| setCustomMarkerStyle | StaticMap | Sets the custom marker image to use when creating new markers. |
| setFormat | StaticMap | Sets the format of the map image. |
| setLanguage | StaticMap | Sets the language to be used for text on the map (where avaialbe). |
| setMapType | StaticMap | Sets the type of map to be shown. |
| setMarkerStyle | StaticMap | Sets the marker style to use when creating new markers. |
| setMobile | StaticMap | Sets whether or not to use specialized tile sets for mobile devices. |
| setPathStyle | StaticMap | Sets the path style to use when creating new paths. |
| setSize | StaticMap | Sets the width and height of the map image in pixels. |
| setZoom | StaticMap | Sets the zoom factor, or magnification level, used for the map. |
Class StaticMapEnums
StaticMapで利用できる列挙型
| プロパティ | タイプ | 簡単な説明 |
| Color | Color | 色を設定 |
| Format | Format | 静止画のファイルタイプを設定 |
| MarkerSize | MarkerSize | マーカーのサイズを設定 |
| Type | Type | マップのタイプを設定 |
列挙型名一覧
列挙型のメソッドを利用するときには、基本的にはEnums用クラスと併用して使います。具体的には『Maps.StaticMap.列挙型名.プロパティ名』or 『Maps.DirectionFinder.列挙型名.プロパティ名』の形で利用します。例えば、AvoidのTOLLSを設定したい場合は、『Maps.DirectionFinder.Avoid.TOLLS』
ColorのBLACKを設定したい場合は、『Maps.staticMap.Color.BLACK』という形で記述します。
Enums Avoid
行き先検索の際に検索条件に制限をかけることができます。
| プロパティ | タイプ | 説明 |
| TOLLS | Enum | 有料道路を避けて検索 |
| HIGHWAYS | Enum | 高速道路を避けて検索 |
Enums Color
マップ画像の中で利用できる色を設定することができます。
| プロパティ | タイプ | 説明 |
| BLACK | Enum | ■ ブラック |
| BROWN | Enum | ■ ブラウン |
| GREEN | Enum | ■ グリーン |
| PURPLE | Enum | ■ パープル |
| YELLOW | Enum | ■ イエロー |
| BLUE | Enum | ■ ブルー |
| GRAY | Enum | ■ グレー |
| ORANGE | Enum | ■ オレンジ |
| RED | Enum | ■ レッド |
| WHITE | Enum | ■ ホワイト |
Enums Format
マップ画像のファイル形式を設定することができます。
| プロパティ | タイプ | 説明 |
| PNG | Enum | 8-bit PNG形式 |
| PNG8 | Enum | 8-bit PNG形式 |
| PNG32 | Enum | 32-bit PNG形式 |
| GIF | Enum | GIF形式 |
| JPG | Enum | プログレッシブJPEG形式 |
| JPG_BASELINE | Enum | ベースラインJPEG形式 |
Enums MarkerSize
マーカーのサイズを設定できます。
| プロパティ | タイプ | 説明 |
| TINY | Enum | 最小サイズの設定で、ラベルは非表示 |
| MID | Enum | 最大サイズの設定で、ラベルは表示 |
| SMALL | Enum | 中サイズの設定で、ラベルは非表示 |
Enums Mode
行き先検索時の交通手段を設定できます。
| プロパティ | タイプ | 説明 |
| DRIVING | Enum | 車で車道を通る条件で道順を検索 |
| WALKING | Enum | 徒歩で歩道や小道を通る条件で道順を検索 |
| BICYCLING | Enum | 自転車で小道や道路を通る条件で道順を検索 |
| TRANSIT | Enum | 公共交通機関を使って道順を検索。このモードでは出発時間か到着時間を一緒に設定する事が必要※ |
//公共交通機関を使った行き先検索関数
function sampleCodeForTransitMode() {
var directions = Maps.newDirectionFinder()
.setOrigin('新宿駅')
.setDestination('羽田空港')
.setMode(Maps.DirectionFinder.Mode.TRANSIT)
.setDepart(new Date())
.getDirections();
var route = directions.routes[0];
console.log(route);
}
Enums Type
マップの表示方法を設定できます。プロパティ名をクリックすると各設定の表示マップ例が確認できます。
| プロパティ | タイプ | 説明 |
| ROADMAP | Enum | Googleマップの標準の表示方法 |
| SATELLITE | Enum | 航空写真表示 |
| TERRAIN | Enum | 標準表示方法に地形がわかるように加工された表示 |
| HYBRID | Enum | 航空写真表示に標準表示の主要道路や主要建物のラベルを付加した表示 |

コメント