提交 7d39fd63 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

refactor(h5 map control): 统一添加方式及优化添加逻辑

上级 2167bfbb
import { inject, onUnmounted, watch, PropType } from 'vue' import { inject, onUnmounted, watch, PropType } from 'vue'
import { getRealPath } from '@dcloudio/uni-platform' import { getRealPath } from '@dcloudio/uni-platform'
import { defineSystemComponent, useCustomEvent } from '@dcloudio/uni-components' import { defineSystemComponent, useCustomEvent } from '@dcloudio/uni-components'
import { Maps, Map, QQMap, GoogleMap } from './maps' import { Maps, Map } from './maps'
import { getIsAMap } from '../../../helpers/location'
import { QQMaps } from './maps/qq/types'
import { GoogleMaps } from './maps/google/types'
interface Position { interface Position {
left: number | string left: number | string
...@@ -41,27 +38,27 @@ export default /*#__PURE__*/ defineSystemComponent({ ...@@ -41,27 +38,27 @@ export default /*#__PURE__*/ defineSystemComponent({
control.remove() control.remove()
} }
} }
onMapReady((map, maps, trigger) => { onMapReady((_, __, trigger) => {
function updateControl(option: Props) { function updateControl(option: Props) {
removeControl() removeControl()
addControl(option) addControl(option)
} }
function addControl(option: Props) { function addControl(option: Props) {
const position = option.position || {}
control = document.createElement('div') control = document.createElement('div')
const img = new Image()
control.appendChild(img)
const style = control.style const style = control.style
style.position = 'absolute' style.position = 'absolute'
style.width = '0' style.width = '0'
style.height = '0' style.height = '0'
style.top = '0' style.top = '0'
style.left = '0' style.left = '0'
const img = new Image()
img.src = getRealPath(option.iconPath)
img.onload = () => { img.onload = () => {
if (option.position.width) { const position = option.position || {}
if (position.width) {
img.width = option.position.width img.width = option.position.width
} }
if (option.position.height) { if (position.height) {
img.height = option.position.height img.height = option.position.height
} }
const style = img.style const style = img.style
...@@ -69,8 +66,10 @@ export default /*#__PURE__*/ defineSystemComponent({ ...@@ -69,8 +66,10 @@ export default /*#__PURE__*/ defineSystemComponent({
style.left = (position.left || 0) + 'px' style.left = (position.left || 0) + 'px'
style.top = (position.top || 0) + 'px' style.top = (position.top || 0) + 'px'
style.maxWidth = 'initial' style.maxWidth = 'initial'
control.appendChild(img)
props.rootRef.value && props.rootRef.value.appendChild(control)
} }
img.src = getRealPath(option.iconPath)
img.onclick = function ($event) { img.onclick = function ($event) {
if (option.clickable) { if (option.clickable) {
trigger('controltap', $event, { trigger('controltap', $event, {
...@@ -78,13 +77,6 @@ export default /*#__PURE__*/ defineSystemComponent({ ...@@ -78,13 +77,6 @@ export default /*#__PURE__*/ defineSystemComponent({
}) })
} }
} }
if (getIsAMap()) {
props.rootRef.value && props.rootRef.value.appendChild(control)
} else {
;(map as QQMap | GoogleMap).controls[
(maps as QQMaps | GoogleMaps).ControlPosition.TOP_LEFT
].push(control)
}
} }
addControl(props as Props) addControl(props as Props)
watch(props, updateControl) watch(props, updateControl)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册