From 238fba3042dc6c99478581254da60d18830592c2 Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Mon, 2 Sep 2019 17:29:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(event):=20add=20touch=20event=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81touch=E4=BA=8B=E4=BB=B6,=E7=A7=BB=E5=8A=A8=E7=AB=AF?= =?UTF-8?q?=E4=BA=A4=E4=BA=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/controller/event.js | 4 ++++ src/core/engine/picking/picking.js | 9 ++++++++- src/core/layer.js | 31 ------------------------------ src/core/scene.js | 4 +++- 4 files changed, 15 insertions(+), 33 deletions(-) diff --git a/src/core/controller/event.js b/src/core/controller/event.js index 941d9a9c47..b36339eb1a 100644 --- a/src/core/controller/event.js +++ b/src/core/controller/event.js @@ -40,6 +40,10 @@ export default class EventContoller { lnglat: { lng: lnglat.lng, lat: lnglat.lat } }; if (featureId >= 0) { // 拾取到元素,或者离开元素 + if (type.substr(0, 5) === 'touch') { + type = 'click'; + this.layer.emit('mousemove', target); + } this.layer.emit(type, target); this._selectedId = featureId; } diff --git a/src/core/engine/picking/picking.js b/src/core/engine/picking/picking.js index 8b6d0dbba8..0abebbf4cf 100755 --- a/src/core/engine/picking/picking.js +++ b/src/core/engine/picking/picking.js @@ -33,7 +33,14 @@ class Picking { window.addEventListener('resize', this._resizeHandler, false); } pickdata(event) { - const point = { x: event.offsetX, y: event.offsetY, type: event.type, _parent: event }; + let point = { x: event.offsetX, y: event.offsetY, type: event.type, _parent: event }; + if (event.type.substr(0, 5) === 'touch') { + if (event.touches.length === 0) { + return; + } + const touch = event.touches[0]; + point = { x: touch.clientX, y: touch.clientY, type: event.type, _parent: touch }; + } const normalisedPoint = { x: 0, y: 0 }; normalisedPoint.x = (point.x / this._width) * 2 - 1; normalisedPoint.y = -(point.y / this._height) * 2 + 1; diff --git a/src/core/layer.js b/src/core/layer.js index a4c37c6000..e66465551d 100644 --- a/src/core/layer.js +++ b/src/core/layer.js @@ -480,37 +480,6 @@ export default class Layer extends Base { } } - _initEvents() { - this.scene.on('pick-' + this.layerId, e => { - let { featureId, point2d, type } = e; - // TODO 瓦片图层获取选中数据信息 - const lnglat = this.scene.containerToLngLat(point2d); - let feature = null; - let style = null; - if (featureId !== -999) { - const res = this.getSelectFeature(featureId, lnglat); - feature = res.feature; - style = res.style; - } - const target = { - featureId, - feature, - style, - pixel: point2d, - type, - lnglat: { lng: lnglat.lng, lat: lnglat.lat } - }; - if (featureId >= 0) { // 拾取到元素,或者离开元素 - this.emit(type, target); - } - if (featureId < 0 && this._activeIds >= 0) { - type = 'mouseleave'; - this.emit(type, target); - } - this._activeIds = featureId; - - }); - } getSelectFeature(featureId, lnglat) { // return {}; if (this.get('layerType') === 'tile') { diff --git a/src/core/scene.js b/src/core/scene.js index 5679344498..19029a0975 100644 --- a/src/core/scene.js +++ b/src/core/scene.js @@ -144,6 +144,9 @@ export default class Scene extends Base { 'mousemove', 'mousedown', 'mouseleave', + 'touchstart', + 'touchmove', + 'touchend', 'mouseup', 'rightclick', 'click', @@ -153,7 +156,6 @@ export default class Scene extends Base { this._container.addEventListener(event, e => { // 要素拾取 if (e.target.nodeName !== 'CANVAS') return; - e.pixel || (e.pixel = e.point); this._engine._picking.pickdata(e); }, true); });