【问题标题】:Can we make custom overlays draggable on google maps V3我们可以在谷歌地图 V3 上制作可拖动的自定义叠加层吗
【发布时间】:2017-10-17 02:32:00
【问题描述】:

标记是可拖动的,但我不能在标记上设置自定义 div,该标记会在悬停单击等时发生变化。因此我想到了使用自定义叠加,但我无法确定自定义叠加是否支持拖动。这个已经有答案了,但是demo本身不行,

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

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

【问题讨论】:

标签: google-maps-api-3


【解决方案1】:

是的,我们可以。

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 版本中,对地图的可拖动选项进行了更改。

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

【讨论】:

  • 这段代码有效(谢谢!),但是基于空格阅读起来很糟糕。值得庆幸的是,有一个 jsfiddle 可以证明它有效,您可以使用“TidyUp”按钮。
  • 它在整个container上绑定了一个mousedown事件,这使得在容器本身或它的子容器上绑定click事件是不可能的。例如,如果容器中有一个<button>,而我想在按钮上绑定一个click 事件,则无法完成。如何在容器内的div.drag-handler 上绑定mousedown(和mouseup)事件,以便拖动此div 将使父容器相应移动?
  • @FelixDing : 我对点击绑定没有问题jsfiddle.net/QRuW8/116
  • @Dr.Molle 谢谢。您可以使用事件侦听器绑定事件吗?喜欢document.getElementById('button'). addEventListener('click', function(){})
  • 不是直接的,因为当你调用构造函数时,内容不是DOM的一部分。但是您可以使用 onAdd 方法来实现它。 jsfiddle.net/QRuW8/119
猜你喜欢
  • 2014-05-22
  • 1970-01-01
  • 2011-08-07
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多