google-maps-api-3 - Google Maps draggable overlay on touch screen
问题描述
I have added a custom draggable overlay onto a google map, which works well using a mouse, but doesn't work on touch screens.
The code I am using is from this question : Can we make custom overlays draggable on google maps V3
The code is from @Dr.Molle and shown below.
Is there anything I can do to make it work on a touch screen? The marker shows, but I cannot move it.
DraggableOverlay.prototype = new google.maps.OverlayView();
DraggableOverlay.prototype.onAdd = function () {
var container = document.createElement('div'),
that = this;
if (typeof this.get('content').nodeName !== 'undefined') {
container.appendChild(this.get('content'));
} else {
if (typeof this.get('content') === 'string') {
container.innerHTML = this.get('content');
} else {
return;
}
}
container.style.position = 'absolute';
container.draggable = true;
google.maps.event.addDomListener(this.get('map').getDiv(),
'mouseleave',
function () {
google.maps.event.trigger(container, 'mouseup');
}
);
google.maps.event.addDomListener(container,
'mousedown',
function (e) {
this.style.cursor = 'move';
that.map.set('draggable', false);
that.set('origin', e);
that.moveHandler =
google.maps.event.addDomListener(that.get('map').getDiv(),
'mousemove',
function (e) {
var origin = that.get('origin'),
left = origin.clientX - e.clientX,
top = origin.clientY - e.clientY,
pos = that.getProjection()
.fromLatLngToDivPixel(that.get('position')),
latLng = that.getProjection()
.fromDivPixelToLatLng(new google.maps.Point(pos.x - left,
pos.y - top));
that.set('origin', e);
that.set('position', latLng);
that.draw();
});
}
);
google.maps.event.addDomListener(container, 'mouseup', function () {
that.map.set('draggable', true);
this.style.cursor = 'default';
google.maps.event.removeListener(that.moveHandler);
});
this.set('container', container)
this.getPanes().floatPane.appendChild(container);
};
function DraggableOverlay(map, position, content) {
if (typeof draw === 'function') {
this.draw = draw;
}
this.setValues({
position: position,
container: null,
content: content,
map: map
});
}
DraggableOverlay.prototype.draw = function () {
var pos = this.getProjection().fromLatLngToDivPixel(this.get('position'));
this.get('container').style.left = pos.x + 'px';
this.get('container').style.top = pos.y + 'px';
};
DraggableOverlay.prototype.onRemove = function () {
this.get('container').parentNode.removeChild(this.get('container'));
this.set('container', null)
};
new DraggableOverlay(
map,//maps-instance
latLng,//google.maps.LatLng
'content',//HTML-String or DOMNode
function(){}//optional, function that ovverrides the draw-method
);
解决方案
推荐阅读
- java - 单击时使用片段进行卡片视图时,活动意图不起作用
- java - GSON 默认日期序列化程序是特定于语言环境的
- angular - 基于数据的角度垫表禁用按钮
- node.js - Facebook 广告活动 Nodejs
- python - 如何从 2017.0、2018.0、2019.0 等中删除 .0?
- flutter - 成员 'setState' 只能在 'package:flutter/src/widgets/framework.dart' 的子类的实例成员中使用
- rust - 从函数返回闭包与就地定义不一致
- export - 如何防止人或程序从系统中提取数据?
- java - 浮点数 1.0E-12F 如何转换为二进制代码?
- sql - 需要从 2 个(或更多)不同的“起始日期”生成“迄今为止”