Map plugin 文件
簡易使用google map api且不依賴jQuery
目錄
引入
<!-- map plugin -->
<script src="<?php echo $http_url_link;?>public/js/map_plugin.js" async></script>
<!---google map api---->
<script src="<?php echo $http_url_link;?>public/js/markerclusterer.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=<?php echo $index_web_mapkey;?>&v=weekly" defer></script>快速使用
※請在onload之後實體化
// 標記資料
var markers = [{
position: [24.1893376,120.6616064],
icon: './images/marker/main.png',
title:'本館位置',
animation: 'BOUNCE',
event: {
creared() {
// 標記建立後 do somethings...
}
click() {
// 標記點擊事件 do somethings...
}
}
}, {
position: [24.1893376,120.6616064],
icon: "./images/marker/current.png",
title: '大雅公園',
animation: 'DROP',
expansion: {
styles: [{}, {}] // 客製化地圖樣式,物件內可以放自訂樣式
}
}];
// 實體化,請在onload之後實體化
var map = new MapPlugin('#spotMap', markers, {
center: [25.0485562, 121.5410635], // or { lat: 25.0485562, lng: 121.5410635 }
infoWindow: true, // 點擊標記彈出視窗
cluster: {
title: '這個標記群組內有{{count}}個標記’, // {{count}}等於標記群組內的標記數量
icon: {
url: 'https://your-image-url.abc',
},
label: {
color: '#a1a1a1'
}
}, // 使用標記群組
});
// 初始化
map.init();
// 路線繪製
var origin = [25.0485562, 121.5410635]; // or { location: { lat: 25.0485562, lng: 121.5410635 } } or { lat: 25.0485562, lng: 121.5410635 }
var waypoints = [{location: [24.143429,120.645212] }];
var destination = [24.1893376,120.6616064];
map.getRoute({
origin, // 起點
waypoints, // 中繼點
destination, // 終點
}, function(res) {
}); // res 參數為回傳路線資訊
// 路線導航繪製,參數帶入selector,會將生成的html放入
map.getRoutePanel('#direction');實體化
new MapPlugin(element, markers, options)
| 參數名稱 | 功能 | 型別 |
|---|---|---|
| element | 必填,地圖的html標籤 | string |
| markers | 必填,標記陣列 | object[] |
| options | 地圖選項物件 | object |
標記選項(markers)
| 屬性名稱 | 功能 | 型別 | 預設值 |
|---|---|---|---|
| id | 標記id,若不填會自動產生 | string | '’ |
| position | 必填,標記座標 | google.maps.LatLngLiteral | google.maps.Place | number[] | [25.0485562, 21.5410635] |
| title | 標記標題,滑鼠懸停會出現 | string | '’ |
| animation | 標記動畫形式,選項有'DROP'、'BOUNCE',若不需使用請使用'' | string | 'DROP’ |
| icon | 自訂icon,請寫入url或使用原生Icon物件內的屬性https://developers.google.com/maps/documentation/javascript/reference/marker#Icon | string | google.maps.Icon | 無 |
| icon.size | 調整icon尺寸,使用方法: new google.maps.Size(10, 10) | Size | 無 |
| infoWindowHTML | 標記彈跳視窗HTML字串,options.infoWindow必須開啟 | string | ‘’ |
| event | 標記事件物件 | object | 無 |
| event.created | 標記建立後觸發,參數內的marker為google原生Marker class | function | function(marker) {} |
| event.click | 標記點擊事件擴充,參數內的marker為google原生Marker class | function | function(marker) {} |
| expansion | 標記的其他google map api原生屬性請放在這個物件內,文件請參考 https://developers.google.com/maps/documentation/javascript/markers | object | 無 |
選項(options)
| 屬性名稱 | 功能 | 型別 | 預設值 |
|---|---|---|---|
| center | 中心點座標,center或markerFitBounds屬性必須至少使用一種 | google.maps.LatLngLiteral | google.maps.Place | number[] | [25.0485562, 121.5410635] |
| markerFitBounds | 依據所有標記自適應位置、縮放,center或markerFitBounds屬性必須至少使用一種 | boolean | false |
| zoom | 縮放,注意若開啟markerFitBounds此功能會失效 | number | 14 |
| disableDefaultUI | 地圖小工具開關 | boolean | false |
| cluster | 標記群組,須確定markers有內容才能使用,若不需客製可直接改成true,不須添加選項 | object | boolean | false |
| cluster.title | 標記群組標題,滑鼠懸停會出現,字串中若包含{{count}},會轉譯成改群組內的標記數量 | string | '這個標記群組內有{{count}}個標記’ |
| cluster.label | 標記群組標籤 | object | { text: '', color: 'rgba(255,255,255,0.9)', fontSize: '12px', } |
| cluster.label.text | 標記群組標籤文字,若不填則為該群組內的標記數量 | string | 改群組內的標記數量 |
| cluster.label.color | 標記群組標籤顏色 | string | 'rgba(255,255,255,0.9)’ |
| cluster.label.fontSize | 標記群組標籤文字大小 | string | '12px’ |
| cluster.icon.svg | 標記群組圖示svg | string | svg標籤 |
| cluster.icon.url | 標記群組圖示連結,若連結和svg同時設定則優先使用svg | string | null |
| cluster.icon.width | 標記群組圖示寬 | number | 45 |
| cluster.icon.height | 標記群組圖示高 | number | 45 |
| mapId | 地圖id,若有在地圖平台自訂義地圖才需要使用 | string | 無 |
| infoWindow | 標記彈跳視窗,注意若開啟此功能,請不要在標記的click事件中使用infoWindow,避免衝突 | boolean | false |
| expansion | 選項擴充物件,若需使用google map api原生屬性請放在這個物件內,文件請參考 https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions | object | 無 |
方法
init()
地圖初始化
updateMarkers(markers)
更新標記
| 參數名稱 | 功能 | 型別 |
|---|---|---|
| markers | 標記陣列,同markers參數格式 | object[] |
openInfoWindow(id)
打開標記視窗,需要在選項(options)中開啟infoWindow才會有效
| 參數名稱 | 功能 | 型別 |
|---|---|---|
| id | 標記id,會自動尋找標記選項內的infoWindowHTML當作視窗內容,並在標記的位置開啟 | object[] |
closeAllInfoWindows()
關閉所有infoWindows
panTo(latLng)
移動中心點到經緯度
| 參數名稱 | 功能 | 型別 |
|---|---|---|
| latLng | 位置資料 | google.maps.LatLngLiteral | google.maps.Place | number[] |
setZoom(num)
設定地圖縮放
| 參數名稱 | 功能 | 型別 |
|---|---|---|
| num | 縮放數值 | number |
getRoute(options, callback)
繪製導航路線
| 參數名稱 | 功能 | 型別 | 預設值 |
|---|---|---|---|
| options | 路線選項,在此只列舉部分,完整選項請參考,https://developers.google.com/maps/documentation/javascript/directions#TravelModes | object | 無 |
| options.origin | 必填,起始點,支援以下格式:地點字串, {lat: 25.0485562, lng: 25.0485562}, [25.0485562, 25.0485562], google.maps.Place | google.maps.LatLngLiteral | string | google.maps.Place | number[] | 無 |
| options.destination | 必填,終點,支援以下格式:地點字串, {lat: 25.0485562, lng: 25.0485562}, [25.0485562, 25.0485562], google.maps.Place | google.maps.LatLngLiteral | string | google.maps.Place | number[] | 無 |
| options.travelMode | 交通方式,支援以下格式:'DRIVING' (開車), 'BICYCLING' (腳踏車), 'TRANSIT' (大眾交通工具), 'WALKING' (步行) | string | 'DRIVING’ |
| options.waypoints | 中繼點 | object[] | 無 |
| options.waypoints[].location | 中繼點位置 | google.maps.LatLngLiteral | string | google.maps.Place | number[] | 無 |
| options.waypoints[].stopover | 是否依照中繼點分割路線 | boolean | 無 |
| options.avoidFerries | 如果可能,避開渡輪 | boolean | 無 |
| options.avoidHighways | 如果可能,避開高速公路 | boolean | 無 |
| options.avoidTolls | 如果可能,收費公路 | boolean | 無 |
| callback | 路線產生後的做的動作,第一個參數為路徑資料 | function | undefined |
getDistanceMatrix(options, callback)
取得距離矩陣,像是兩點之間距離、時間等等(最多一次搜尋25個位置)
| 參數名稱 | 功能 | 型別 | 預設值 |
|---|---|---|---|
| options | 距離矩陣,在此只列舉部分,完整選項請參考,https://developers.google.com/maps/documentation/javascript/distancematrix#distance_matrix_requests | object | 無 |
| options.origins | 起始點陣列 | object[]|array[] | 無 |
| options.origins[] | 起始點陣列內的單個標記資訊,Place物件https://developers.google.com/maps/documentation/javascript/reference/directions#Place | google.maps.LatLngLiteral | string | google.maps.Place | number[] | 無 |
| options.destinations | 終點陣列 | object[]|array[] | |
| options.destinations[] | 終點陣列內的單個標記資訊,Place物件https://developers.google.com/maps/documentation/javascript/reference/directions#Place | google.maps.LatLngLiteral | string | google.maps.Place | number[] | 無 |
| options.travelMode | 交通方式,支援以下格式:'DRIVING' (開車), 'BICYCLING' (腳踏車), 'TRANSIT' (大眾交通工具), 'WALKING' (步行) | string | 'DRIVING’ |
| options.avoidFerries | 如果可能,避開渡輪 | boolean | 無 |
| options.avoidHighways | 如果可能,避開高速公路 | boolean | 無 |
| options.avoidTolls | 如果可能,收費公路 | boolean | 無 |
| callback | 路線產生後的做的動作,第一個參數為距離矩陣資料 | function | undefined |
setIcon(id, icon)
修改標記icon
| 參數名稱 | 功能 | 型別 |
|---|---|---|
| id | 標記id | string |
| icon | 連結或icon物件 | string | google.maps.Icon |
setAllIcon(iconArray)
根據標記的id設定所有icon
| 參數名稱 | 功能 | 型別 |
|---|---|---|
| iconArray | icon陣列 | object[] |
| iconArray[].id | 標記 id | string |
| iconArray[].icon | 連結或icon物件 | string | google.maps.Icon |
findMarkerById(id)
使用id取得原生Marker class
| 回傳值 | 功能 | 型別 |
|---|---|---|
| marker | google原生Marker class | google.maps.Marker |
getCenter()
取得中心座標
| 回傳值 | 功能 | 型別 |
|---|---|---|
| LatLng | 座標資料 | google.maps.LatLngLiteral | undefined |
2022 by Calvin v.1.0