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

fix(map): 修复高德地图h5PC端覆盖物点击触发map点击问题

上级 cc623a38
...@@ -96,17 +96,18 @@ export default { ...@@ -96,17 +96,18 @@ export default {
}) })
this.$parent._markers[this.idString] = marker this.$parent._markers[this.idString] = marker
this.updateMarker(props) this.updateMarker(props)
if (IS_AMAP) {
// 不通过 addListener 方式绑定事件,为了规避高德地图覆盖物点击触发map点击问题
marker.dom.addEventListener('click', e => {
this.handleAMapMarkerClick(e, marker)
})
marker.dom.addEventListener('touchend', e => {
this.handleAMapMarkerClick(e, marker)
})
} else {
maps.event.addListener(marker, 'click', (e) => { maps.event.addListener(marker, 'click', (e) => {
const callout = marker.callout const callout = marker.callout
if (callout && !callout.alwaysVisible) { if (callout && !callout.alwaysVisible) {
if (IS_AMAP) {
callout.visible = !callout.visible
if (callout.visible) {
marker.callout.createAMapText()
} else {
marker.callout.removeAMapText()
}
} else {
callout.set('visible', !callout.visible) callout.set('visible', !callout.visible)
if (callout.visible) { if (callout.visible) {
const div = callout.div const div = callout.div
...@@ -115,9 +116,8 @@ export default { ...@@ -115,9 +116,8 @@ export default {
parent.appendChild(div) parent.appendChild(div)
} }
} }
}
const event = e.event || e.domEvent || e.originEvent const event = e.event || e.domEvent
if (this.idString) { if (this.idString) {
const { latitude, longitude } = this.getMarkerLatitudeLongitude(e) const { latitude, longitude } = this.getMarkerLatitudeLongitude(e)
...@@ -127,10 +127,7 @@ export default { ...@@ -127,10 +127,7 @@ export default {
longitude longitude
}) })
} }
// 避开高德地图bug: pc端 stopPropagation 无效且地图随鼠标移动
if (event !== e.originEvent) {
event.stopPropagation() event.stopPropagation()
}
}) })
// 处理 google H5移动端 maker 点击触发 map 点击问题 // 处理 google H5移动端 maker 点击触发 map 点击问题
maps.event.addListener(marker, 'mousedown', (e) => { maps.event.addListener(marker, 'mousedown', (e) => {
...@@ -138,6 +135,7 @@ export default { ...@@ -138,6 +135,7 @@ export default {
e.domEvent.stopPropagation() e.domEvent.stopPropagation()
} }
}) })
}
}, },
updateMarker (option) { updateMarker (option) {
const map = this._map const map = this._map
...@@ -318,6 +316,25 @@ export default { ...@@ -318,6 +316,25 @@ export default {
console.error('Marker.iconPath is required.') console.error('Marker.iconPath is required.')
} }
}, },
handleAMapMarkerClick (e, marker) {
const callout = marker.callout
if (callout && !callout.alwaysVisible) {
callout.visible = !callout.visible
if (callout.visible) {
marker.callout.createAMapText()
} else {
marker.callout.removeAMapText()
}
}
if (this.idString) {
this.$parent.$trigger('markertap', e, {
markerId: Number(this.idString),
latitude: marker._position.lat,
longitude: marker._position.lng
})
}
e.stopPropagation()
},
updateMarkerLabelStyle (id, style) { updateMarkerLabelStyle (id, style) {
const className = 'uni-map-marker-label-' + id const className = 'uni-map-marker-label-' + id
let styleEl = document.getElementById(className) let styleEl = document.getElementById(className)
......
...@@ -32,14 +32,22 @@ export function createCallout (maps) { ...@@ -32,14 +32,22 @@ export function createCallout (maps) {
}, },
position: option.position position: option.position
}) })
// 不通过 addListener 方式绑定事件,为了规避高德地图覆盖物点击触发map点击问题
maps.event.addListener(this.Text, 'click', ({ originEvent }) => { this.Text.dom.addEventListener('click', e => {
this.callback(originEvent, this.parent) handleAMapTextClick(this, e)
})
this.Text.dom.addEventListener('touchend', e => {
handleAMapTextClick(this, e)
}) })
this.Text.setMap(option.map) this.Text.setMap(option.map)
} }
function handleAMapTextClick (self, e) {
self.callback(e, self.parent)
e.stopPropagation()
}
function removeAMapText () { function removeAMapText () {
if (this.Text) { if (this.Text) {
this.option.map.remove(this.Text) this.option.map.remove(this.Text)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册