openlayers地图上要素拖动交互

参考代码:

//拖动交互
function addMoveInteraction(){
	var app = {};
	app.Drag = function() {

        ol.interaction.Pointer.call(this, {
          handleDownEvent: app.Drag.prototype.handleDownEvent,
          handleDragEvent: app.Drag.prototype.handleDragEvent,
          handleMoveEvent: app.Drag.prototype.handleMoveEvent,
          handleUpEvent: app.Drag.prototype.handleUpEvent
        });

        this.coordinate_ = null;

        this.cursor_ = 'pointer';

        this.feature_ = null;

        this.previousCursor_ = undefined;

      };
      ol.inherits(app.Drag, ol.interaction.Pointer);

      app.Drag.prototype.handleDownEvent = function(evt) {
        var map = evt.map;

        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function(feature) {
              return feature;
            });

        if (feature) {
        	 var geom = (feature.getGeometry());
             if (geom instanceof ol.geom.MultiPolygon) {
               return;
             } else if (geom instanceof ol.geom.LineString) {
               return;
             }else{
             	 this.coordinate_ = evt.coordinate;
            // alert(evt.coordinate[0]);
             this.feature_ = feature;
             }
        }
        return !!feature;
      };

      app.Drag.prototype.handleDragEvent = function(evt) {
        var deltaX = evt.coordinate[0] - this.coordinate_[0];
        var deltaY = evt.coordinate[1] - this.coordinate_[1];

        var geometry = this.feature_.getGeometry();
        geometry.translate(deltaX, deltaY);

        this.coordinate_[0] = evt.coordinate[0];
        this.coordinate_[1] = evt.coordinate[1];
      };

      app.Drag.prototype.handleMoveEvent = function(evt) {
      	
        if (this.cursor_) {
          var map = evt.map;
          var feature = map.forEachFeatureAtPixel(evt.pixel,
              function(feature) {
              	//alert("handleMoveEvent");
                return feature;
              });
          var element = evt.map.getTargetElement();
          if (feature) {
            if (element.style.cursor != this.cursor_) {
              this.previousCursor_ = element.style.cursor;
              element.style.cursor = this.cursor_;
            }
          } else if (this.previousCursor_ !== undefined) {
            element.style.cursor = this.previousCursor_;
            this.previousCursor_ = undefined;
          }
        }
      };

      app.Drag.prototype.handleUpEvent = function(evt) {
    	  //拖动以后触发操作
    	var stationnum = this.feature_.U.StationNum;
        this.coordinate_ = null;
        this.feature_ = null;
        //alert("handleUpEvent"+evt.coordinate[1]);
        var laAndLo = excuteExtent(evt);
        
    	stationnumArr.push(stationnum);
    	LoAndLaArr.push(laAndLo);
        //updateLoAnaLa(laAndLo,stationnum);
        
        return false;
      };
      appD = new app.Drag();
      //将交互添加到map中
	map.addInteraction(appD);
}

转载自:https://blog.csdn.net/dong__xue/article/details/80117311

You may also like...