gpt4 book ai didi

google-maps-api-3 - 我们可以在谷歌地图 V3 上制作可拖动的自定义叠加层吗

转载 作者:行者123 更新时间:2023-12-04 16:39:42 25 4
gpt4 key购买 nike

标记是可拖动的,但我不能在标记上设置自定义 div,它会在悬停单击等时发生变化。因此我想使用自定义覆盖,但我无法确定自定义覆盖是否支持拖动。已经有答案了,但是演示本身不起作用,

how to make a Custom Overlays draggable using google-maps v3

我没有在 overlayView 类的代码引用中找到任何内容。

最佳答案

我们可以。

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)
};

它观察 mousemove-event 并根据与最后一个鼠标位置的距离修改覆盖的顶部/左侧样式。

用法:
new DraggableOverlay( 
map,//maps-instance
latLng,//google.maps.LatLng
'content',//HTML-String or DOMNode
function(){}//optional, function that ovverrides the draw-method
);

默认情况下,覆盖的左上角将放置在 latLng 提供的位置。 -争论。

要应用自定义绘图,请使用构造函数的可选绘图参数。

演示: http://jsfiddle.net/doktormolle/QRuW8/

编辑:
此解决方案仅适用于 google maps api 的 3.27 版。
在 3.28 版本中,对 map 的可拖动选项进行了更改。

发行说明: https://developers.google.com/maps/documentation/javascript/releases#328

关于google-maps-api-3 - 我们可以在谷歌地图 V3 上制作可拖动的自定义叠加层吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22127626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com