Google Map API 使用手冊
什麼是 Google Map API
「Google 地圖 API」可讓您利用 JavaScript 將「Google 地圖」嵌入自己的網頁。此 API 透過多種服務提供一些公用程式,以操控地圖 (如同 http://maps.google.com 網頁上所示),並新增地圖內容,能讓您在網站上建立強大的地圖應用程式。——Google 地圖 API 的使用方法
開始使用
- 在index.html加入,地圖會顯示在這個標籤裡。
2. 再加上這行引用,請寫在所有script的最上層。
3. 在<body>
插入<script>
成對標籤,並寫入function initMap()
。
4. 新增index.css,寫入以下樣式:
記得一定要寫出height,才會看到地圖出現在網頁上。
5. 瀏覽器展示成果。

初始化地圖相關設定(initMap)
- 修改initMap()
一 其他補充
- polylineOptions:對座標與行車路線外觀與顏色進行改變。
參數名稱 |
代表意思 |
fillColor |
簡單多邊形 (Simple Polygon) 覆蓋區域顏色 |
strokeColor |
行車路線線條顏色 |
strokeOpacity |
行車路線線條透明度,數值越低透明越高,反之。 |
strokeWeight |
行車路線線條寬度,數值越低線條越細,反之。 |
參數名稱 |
代表意思 |
suppressMarkers |
隱藏座標 |
draggable |
是否可以拖曳,更改行車路線。 |
- google.maps.Map:預設載入地圖時的相關設定,會寫在這裡。
參數名稱 |
代表意思 |
center |
地圖預設出現的經緯度位置,有很多種不同的寫法,也可以寫成:{lat:24.0861162, lng: 120.6933002}這種格式。 |
zoom |
載入地圖後,預設的縮放值。 |
mapTypeControl |
街景服務。 |
streetViewControl |
地圖樣式。 |
setMap |
加入地圖圖層。 |
注意mapTypeControl和streetViewControl要關閉(false),避免被而外收取費用。
標示座標點(Marker)與製作資訊視窗(InfoWindow)
- 在最外圈宣告全域變數
markers
與infoWindows
markers
用來儲存座標點。
new google.maps.Marker
儲存座標的資訊視窗內容。
2. 新增加markWaypointMap
的function。
new google.maps.Marker
根據e內容產生座標點。
new google.maps.InfoWindow
根據content內容產生資訊視窗。
3. 在initMap()加入,markWaypointMap()
呼叫函式。
4. 瀏覽器展示成果 ( 可依需求再加上樣式 ) 。

標記叢集(MarkerClusterer)
- 新增加
zoomInOutMap
的function。
markerClusterer.MarkerClusterer
管理markers製造出標記叢集。
- 在initMap()加入,
zoomInOutMap()
呼叫函式。
3. 瀏覽器展示成果。

畫行車路線(setDirections)
- 在全域變數宣告
marker_config
,用來儲存經過點,行車路線會經過這些座標點。
var marker_config = [
{
"id": 1,
"name": "台灣大道一段",
"site": "400台中市中區台灣大道一段1號鐵鹿大街A10",
"type": "Point",
"position": {
lat: 24.1372369,
lng: 120.68687
}
},
{
"id": 2,
"name": "台灣大道一段",
"site": "400台中市中區台灣大道一段1號",
"type": "Point",
"position": {
lat: 24.1370597,
lng: 120.6869718
}
},
{
"id": 3,
"name": "台灣大道一段",
"site": "400台中市中區台灣大道一段1號B3櫃位",
"type": "Point",
"position": {
lat: 24.1373346,
lng: 120.6872849
}
},
{
"id": 4,
"name": "新民街",
"site": "401台中市東區新民街88號",
"type": "Point",
"position": {
lat: 24.137648,
lng: 120.686498
}
},
{
"id": 5,
"name": "復興路四段",
"site": "401台中市東區復興路四段186號",
"type": "Point",
"position": {
lat: 24.136575,
lng: 120.6862517
}
},
{
"id": 6,
"name": "公園東路",
"site": "401台中市東區公園東路150號",
"type": "Point",
"position": {
lat: 24.1397534,
lng: 120.683731
}
},
{
"id": 7,
"name": "自由路二段",
"site": "404台中市北區自由路二段湖心亭",
"type": "Point",
"position": {
lat: 24.1397534,
lng: 120.683731
}
},
{
"id": 8,
"name": "南京路",
"site": "4F, No. 66號南京路東區台中市401",
"type": "Point",
"position": {
lat: 24.1361803,
lng: 120.6883141
}
},
{
"id": 9,
"name": "樂業一路",
"site": "401台中市東區樂業一路79號",
"type": "Point",
"position": {
lat: 24.1348453,
lng: 120.6907701
}
},
{
"id": 10,
"name": "林森路",
"site": "403台中市西區林森路100巷",
"type": "Point",
"position": {
lat: 24.1223663,
lng: 120.6662814
}
},
{
"id": 11,
"name": "民生路",
"site": "40358台中市西區民生路368巷2弄12號",
"type": "Point",
"position": {
lat: 24.1319946,
lng: 120.6610709
}
},
{
"id": 12,
"name": "興大路",
"site": "402台中市南區興大路145號",
"type": "Point",
"position": {
lat: 24.1218507,
lng: 120.6742067
}
},
{
"id": 13,
"name": "忠孝路",
"site": "402台中市南區忠孝路",
"type": "Point",
"position": {
lat: 24.1292282,
lng: 120.6741345
}
},
{
"id": 14,
"name": "育德路",
"site": "404台中市北區育德路2號",
"type": "Point",
"position": {
lat: 24.1507654,
lng: 120.6792589
}
},
{
"id": 15,
"name": "館前路",
"site": "404台中市北區館前路1號",
"type": "Point",
"position": {
lat: 24.1417558,
lng: 120.6627565
}
},
{
"id": 16,
"name": "文昌東十一街",
"site": "406台中市北屯區文昌東十一街14巷1號",
"type": "Point",
"position": {
lat: 24.1437317,
lng: 120.677408
}
}
];
- 新增加
vehicleRouteMap
的function。
waypoint
將marker_config的座標位置儲存。
optimizeWaypoints
指定使用提供的路線waypoints可以通過以更有效的順序重新排列航點來優化。如果是true ,方向服務將waypoints 在waypoint_order字段中返回重新排序的內容 一 [官方介紹]。
一 travelMode
補充
參數名稱 |
代表意思 |
BICYCLING |
起點到終點的移動方式為腳踏車 |
DRIVING |
起點到終點的移動方式為開車 |
WALKING |
起點到終點的移動方式為走路 |
TRANSIT |
起點到終點的移動方式為轉運 |
- 在initMap()加入,
vehicleRouteMap()
呼叫函式。
4. 瀏覽器展示成果。

根據zoom縮放呈現不同marker個數
-
因客戶需求在不同的縮放數值(getZoom()),需呈現不同的marker數量,伴隨的資訊視窗顯示內容也不同,因此會在原先的zoomInOutMap() function加入判斷式和新增兩個json檔案。
-
新增 10.json 和 16.json。


將縮放數值顯示不同座標與資訊內容的json data個別存到json檔案。
3. 新增function zoomInOutMarkerMap(),讀取10.json 與 16.json資料,標記座標與產生資訊視窗。
- 新增function deleteMarkersMap(),用來清空上次的座標標記。
- 修改function zoomInOutMap()內容。
- 條件設定在當.getZoom()為 10 或者是 16 時,function裡的條件式會重新判斷,呼叫哪個function做重新標記的動作以及清空上次的標記。
- 瀏覽器展示成果。


