webpack-bootstrap-ui-kit/src/js/drivers/map.google.marker.js

225 lines
6.1 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
'use strict'
2022-05-03 21:44:33 +02:00
const google = window.google
2021-08-09 18:04:09 +02:00
const Obj = {
init: () => {
class GoogleMapsHtmlOverlay extends google.maps.OverlayView {
2022-05-03 20:50:57 +02:00
constructor (options) {
super()
const ui = this
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
ui.ownerMap = options.map
// ui.setMap(options.map);
ui.position = options.position
2021-08-18 20:51:15 +02:00
ui.html = options.html
? options.html
2022-05-03 20:50:57 +02:00
: '<div class="mapboxgl-marker"><i class="marker-icon fas fa-map-marker-alt"></i></div>'
ui.divClass = options.divClass
ui.align = options.align
ui.isDebugMode = options.debug
ui.onClick = options.onClick
ui.onMouseOver = options.onMouseOver
2021-08-09 18:04:09 +02:00
ui.isBoolean = (arg) => {
2022-05-03 20:50:57 +02:00
if (typeof arg === 'boolean') {
return true
2021-08-09 18:04:09 +02:00
} else {
2022-05-03 20:50:57 +02:00
return false
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
}
2021-08-09 18:04:09 +02:00
ui.isNotUndefined = (arg) => {
2022-05-03 20:50:57 +02:00
if (typeof arg !== 'undefined') {
return true
2021-08-09 18:04:09 +02:00
} else {
2022-05-03 20:50:57 +02:00
return false
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
}
2021-08-09 18:04:09 +02:00
ui.hasContent = (arg) => {
if (arg.length > 0) {
2022-05-03 20:50:57 +02:00
return true
2021-08-09 18:04:09 +02:00
} else {
2022-05-03 20:50:57 +02:00
return false
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
}
2021-08-09 18:04:09 +02:00
ui.isString = (arg) => {
2022-05-03 20:50:57 +02:00
if (typeof arg === 'string') {
return true
2021-08-09 18:04:09 +02:00
} else {
2022-05-03 20:50:57 +02:00
return false
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
}
2021-08-09 18:04:09 +02:00
ui.isFunction = (arg) => {
2022-05-03 20:50:57 +02:00
if (typeof arg === 'function') {
return true
2021-08-09 18:04:09 +02:00
} else {
2022-05-03 20:50:57 +02:00
return false
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
}
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
onAdd () {
const ui = this
2021-08-09 18:04:09 +02:00
// Create div element.
2022-05-03 20:50:57 +02:00
ui.div = document.createElement('div')
ui.div.style.position = 'absolute'
2021-08-09 18:04:09 +02:00
// Validate and set custom div class
2022-05-03 20:50:57 +02:00
if (ui.isNotUndefined(ui.divClass) && ui.hasContent(ui.divClass)) { ui.div.className = ui.divClass }
2021-08-09 18:04:09 +02:00
// Validate and set custom HTML
if (
ui.isNotUndefined(ui.html) &&
2021-08-18 20:51:15 +02:00
ui.hasContent(ui.html) &&
ui.isString(ui.html)
2022-05-03 20:50:57 +02:00
) { ui.div.innerHTML = ui.html }
2021-08-09 18:04:09 +02:00
// If debug mode is enabled custom content will be replaced with debug content
if (ui.isBoolean(ui.isDebugMode) && ui.isDebugMode) {
2022-05-03 20:50:57 +02:00
ui.div.className = 'debug-mode'
2021-08-09 18:04:09 +02:00
ui.div.innerHTML =
2021-08-18 20:51:15 +02:00
'<div style="height: 10px; width: 10px; background: red; border-radius: 100%;"></div>' +
2022-05-03 20:50:57 +02:00
'<div style="position: absolute; top: 5px; padding: 5px; width: 130px; text-align: center; font-size: 18px; text-transform: uppercase; font-weight: bolder; background: red; color: white; font-family: Arial;">Debug mode</div>'
2021-08-09 18:04:09 +02:00
ui.div.setAttribute(
2022-05-03 20:50:57 +02:00
'style',
'position: absolute;' +
'border: 5px dashed red;' +
'height: 150px;' +
'width: 150px;' +
'display: flex;' +
'justify-content: center;' +
'align-items: center;'
)
2021-08-09 18:04:09 +02:00
}
// Add element to clickable layer
2022-05-03 20:50:57 +02:00
ui.getPanes().overlayMouseTarget.appendChild(ui.div)
2021-08-09 18:04:09 +02:00
// Add listeners to the element.
2022-05-03 20:50:57 +02:00
google.maps.event.addDomListener(ui.div, 'click', (event) => {
google.maps.event.trigger(ui, 'click')
if (ui.isFunction(ui.onClick)) ui.onClick()
event.stopPropagation()
})
google.maps.event.addDomListener(ui.div, 'mouseover', (event) => {
google.maps.event.trigger(ui, 'mouseover')
if (ui.isFunction(ui.onMouseOver)) ui.onMouseOver()
event.stopPropagation()
})
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
draw () {
const ui = this
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
let div = document.querySelector('.popup')
2021-08-09 18:04:09 +02:00
if (!div.length) {
2022-05-03 20:50:57 +02:00
div = ui.div
2021-08-09 18:04:09 +02:00
}
// Calculate position of div
2022-05-03 20:50:57 +02:00
const projection = ui.getProjection()
2021-08-09 18:04:09 +02:00
if (!projection) {
2022-05-03 20:50:57 +02:00
console.log('GoogleMapsHtmlOverlay: current map is missing')
return null
2021-08-09 18:04:09 +02:00
}
2021-08-18 20:51:15 +02:00
const positionInPixels = projection.fromLatLngToDivPixel(
ui.getPosition()
2022-05-03 20:50:57 +02:00
)
2021-08-09 18:04:09 +02:00
// Align HTML overlay relative to original position
const offset = {
y: undefined,
x: undefined,
2022-05-03 20:50:57 +02:00
}
const divWidth = div.offsetWidth
const divHeight = div.offsetHeight
switch (Array.isArray(ui.align) ? ui.align.join(' ') : '') {
case 'left top':
offset.y = divHeight
offset.x = divWidth
break
case 'left center':
offset.y = divHeight / 2
offset.x = divWidth
break
case 'left bottom':
offset.y = 0
offset.x = divWidth
break
case 'center top':
offset.y = divHeight
offset.x = divWidth / 2
break
case 'center center':
offset.y = divHeight / 2
offset.x = divWidth / 2
break
case 'center bottom':
offset.y = 0
offset.x = divWidth / 2
break
case 'right top':
offset.y = divHeight
offset.x = 0
break
case 'right center':
offset.y = divHeight / 2
offset.x = 0
break
case 'right bottom':
offset.y = 0
offset.x = 0
break
2021-08-09 18:04:09 +02:00
default:
2022-05-03 20:50:57 +02:00
offset.y = divHeight / 2
offset.x = divWidth / 2
break
2021-08-09 18:04:09 +02:00
}
// Set position
2022-05-03 20:50:57 +02:00
ui.div.style.top = `${positionInPixels.y - offset.y}px`
ui.div.style.left = `${positionInPixels.x - offset.x}px`
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
getPosition () {
const ui = this
return new google.maps.LatLng(ui.position)
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
getDiv () {
const ui = this
return ui.div
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
setPosition (position, align) {
const ui = this
ui.position = position
ui.align = align
ui.draw()
2021-08-09 18:04:09 +02:00
}
2022-05-03 20:50:57 +02:00
remove () {
const ui = this
ui.setMap(null)
ui.div.remove()
2021-08-09 18:04:09 +02:00
}
// emulate google.maps.Marker functionality for compatibility (for example with @googlemaps/markerclustererplus)
2022-05-03 20:50:57 +02:00
getDraggable () {
return false
2021-08-09 18:04:09 +02:00
}
}
2022-05-03 20:50:57 +02:00
return GoogleMapsHtmlOverlay
2021-08-09 18:04:09 +02:00
},
2022-05-03 20:50:57 +02:00
}
2021-08-09 18:04:09 +02:00
2022-05-03 20:50:57 +02:00
export default Obj