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#Iconstring | 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 classfunctionfunction(marker) {}
event.click標記點擊事件擴充,參數內的marker為google原生Marker classfunctionfunction(marker) {}
expansion標記的其他google map api原生屬性請放在這個物件內,文件請參考 https://developers.google.com/maps/documentation/javascript/markersobject

選項(options)

屬性名稱功能型別預設值
center中心點座標,center或markerFitBounds屬性必須至少使用一種google.maps.LatLngLiteral | google.maps.Place | number[][25.0485562, 121.5410635]
markerFitBounds依據所有標記自適應位置、縮放,center或markerFitBounds屬性必須至少使用一種booleanfalse
zoom縮放,注意若開啟markerFitBounds此功能會失效number14
disableDefaultUI地圖小工具開關booleanfalse
cluster標記群組,須確定markers有內容才能使用,若不需客製可直接改成true,不須添加選項object | booleanfalse
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標記群組圖示svgstringsvg標籤
cluster.icon.url標記群組圖示連結,若連結和svg同時設定則優先使用svgstringnull
cluster.icon.width標記群組圖示寬number45
cluster.icon.height標記群組圖示高number45
mapId地圖id,若有在地圖平台自訂義地圖才需要使用string
infoWindow標記彈跳視窗,注意若開啟此功能,請不要在標記的click事件中使用infoWindow,避免衝突booleanfalse
expansion選項擴充物件,若需使用google map api原生屬性請放在這個物件內,文件請參考 https://developers.google.com/maps/documentation/javascript/reference/map#MapOptionsobject

方法

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#TravelModesobject
options.origin必填,起始點,支援以下格式:地點字串, {lat: 25.0485562, lng: 25.0485562}, [25.0485562, 25.0485562], google.maps.Placegoogle.maps.LatLngLiteral | string | google.maps.Place | number[]
options.destination必填,終點,支援以下格式:地點字串, {lat: 25.0485562, lng: 25.0485562}, [25.0485562, 25.0485562], google.maps.Placegoogle.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路線產生後的做的動作,第一個參數為路徑資料functionundefined

getDistanceMatrix(options, callback)

取得距離矩陣,像是兩點之間距離、時間等等(最多一次搜尋25個位置)

參數名稱功能型別預設值
options距離矩陣,在此只列舉部分,完整選項請參考,https://developers.google.com/maps/documentation/javascript/distancematrix#distance_matrix_requestsobject
options.origins起始點陣列object[]|array[]
options.origins[]起始點陣列內的單個標記資訊,Place物件https://developers.google.com/maps/documentation/javascript/reference/directions#Placegoogle.maps.LatLngLiteral | string | google.maps.Place | number[]
options.destinations終點陣列object[]|array[]
options.destinations[]終點陣列內的單個標記資訊,Place物件https://developers.google.com/maps/documentation/javascript/reference/directions#Placegoogle.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路線產生後的做的動作,第一個參數為距離矩陣資料functionundefined

setIcon(id, icon)

修改標記icon

參數名稱功能型別
id標記idstring
icon連結或icon物件string | google.maps.Icon

setAllIcon(iconArray)

根據標記的id設定所有icon

參數名稱功能型別
iconArrayicon陣列object[]
iconArray[].id標記 idstring
iconArray[].icon連結或icon物件string | google.maps.Icon

findMarkerById(id)

使用id取得原生Marker class

回傳值功能型別
markergoogle原生Marker classgoogle.maps.Marker

getCenter()

取得中心座標

回傳值功能型別
LatLng座標資料google.maps.LatLngLiteral | undefined

2022 by Calvin v.1.0