zoukankan      html  css  js  c++  java
  • openlayers4实现拖动Feature效果

    拖动矢量图层feature,获取拖动后feature

    官网拓展了interaction,增加了Drag的Interaction

    代码说明

    *handleDragEvent注册的这个事件里面是拖动后获取到的feature,
    handleDownEvent这是鼠标点击feature时的事件,可以进行对拖拽的图形进行限制*

    /**
     * Define a namespace for the application.
     */
    var app = {};
    
    
    /**
     * @constructor
     * @extends {ol.interaction.Pointer}
     */
    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
        });
    
        /**
         * @type {ol.Pixel}
         * @private
         */
        this.coordinate_ = null;
    
        /**
         * @type {string|undefined}
         * @private
         */
        this.cursor_ = 'pointer';
    
        /**
         * @type {ol.Feature}
         * @private
         */
        this.feature_ = null;
    
        /**
         * @type {string|undefined}
         * @private
         */
        this.previousCursor_ = undefined;
    
    };
    ol.inherits(app.Drag, ol.interaction.Pointer);
    
    
    /**
     * @param {ol.MapBrowserEvent} evt Map browser event.
     * @return {boolean} `true` to start the drag sequence.
     */
    app.Drag.prototype.handleDownEvent = function(evt) {
        var map = evt.map;
    
        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function(feature) {
                return feature;
            }, {
                layerFilter: function(l) {//限制可拖动图层,可根据实际需求进行过滤
                    if (l.getProperties().name == 'locatelayer') {
                        return true;
                    } else {
                        return false;
                    }
                }
    
            });
    
        if (feature) {
            this.coordinate_ = evt.coordinate;
            this.feature_ = feature;
        }
    
        return !!feature;
    };
    
    
    /**
     * @param {ol.MapBrowserEvent} evt Map browser event.
     */
    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);
    
      //此时的geometry就是拖动后的geometry
    };
    
    
    /**
     * @param {ol.MapBrowserEvent} evt Event.
     */
    app.Drag.prototype.handleMoveEvent = function(evt) {
        if (this.cursor_) {
            var map = evt.map;
            var feature = map.forEachFeatureAtPixel(evt.pixel,
                function(feature) {
                    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;
            }
        }
    };
    
    
    /**
     * @return {boolean} `false` to stop the drag sequence.
     */
    app.Drag.prototype.handleUpEvent = function() {
        this.coordinate_ = null;
        this.feature_ = null;
        return false;
    };
    //初始化地图时,初始化interaction
    var map = new ol.Map({
        target: 'mapContent',
        interactions: ol.interaction.defaults().extend([new app.Drag()]), 
        controls: ol.control.defaults({
            attribution: false,
            zoom: false
        }),
        layers: [baidumap, sectionLayer, locateLayer],
        view: view
    });
    GIS开发https://www.giserdqy.comGIS,WebGIS,ArcGIS,OpenLayers,Leaflet,Geoserver,PostGIS,BIM,空间大数据,GeoAI技术分享
  • 相关阅读:
    DPDK安装方法 17.12.13
    numa.h:No such file or directory 解决方法
    17秋 软件工程 第六次作业 Beta冲刺 Scrum3
    17秋 软件工程 第六次作业 Beta冲刺 总结博客
    17秋 软件工程 第六次作业 Beta冲刺 Scrum2
    Paper Reviews and Presentations
    17秋 软件工程 第六次作业 Beta冲刺 Scrum1
    17秋 软件工程 第六次作业 Beta冲刺
    error: could not create '/System/Library/Frameworks/Python.framework/Versions/2.7/share': Operation not permitted
    17秋 软件工程 个人作业 软件产品案例分析
  • 原文地址:https://www.cnblogs.com/dqygiser/p/9215834.html
Copyright © 2011-2022 走看看