簡體   English   中英

將事件綁定到openlayer3映射

[英]Binding event to a openlayer3 map

我的網頁中有一個div,其中包含許多標簽,每個標簽中都包含一個openlayer映射。

問題是當我使用以下代碼將click事件附加到地圖時,它不起作用。

//create map in given div ID//
    createMap : function(id) {

         var _this = this;//To refer this dojo AMD module
         //Array of maps.
         //_this.index to track the total no. of maps.
        _this.maps[_this.index] = new ol.Map({
            controls: [],
            interactions: [],
            //layers: [_this.raster],
            target: id,
            view: new ol.View({
                center: [0, 0],
                zoom: 4,
            })
        });

        //get the latlong on
        //map click


        var thisMap = _this.maps[_this.index];
         //Attaching the event.
        _this.maps[_this.index].on("click", function(evt){
            console.log(evt);
        });
    }

但是當我使用以下代碼附加事件時,它的工作正常。

//create map in given div ID//
    createMap : function(id) {

         var _this = this;//To refer this dojo AMD module
         //Array of maps.
         //_this.index to track the total no. of maps.
        _this.maps[_this.index] = new ol.Map({
            controls: [],
            interactions: [],
            //layers: [_this.raster],
            target: id,
            view: new ol.View({
                center: [0, 0],
                zoom: 4,
            })
        });

        //get the latlong on
        //map click


        var thisMap = _this.maps[_this.index];

        //Attaching the event

        _this.maps[_this.index].getViewport().addEventListener("click", function(evt){
            console.log(evt);
        });
    }

但是我不想使用第二種方法注冊事件,因為第一種方法是openlayer的一部分,我可以使用mouseEvent參數做很多事情,例如我可以直接獲取經度和緯度。

早先兩個都可以正常工作,但是在第一種方法停止工作后,我使用dojo小部件重新創建了GUI。

誰能幫我什么忙? 或使用第一種方法還需要什么其他東西?

嘗試這種方式:

createMap : function(id) {

    var thisMap = new ol.Map({
        controls: [],
        interactions: [],
        //layers: [_this.raster],
        target: id,
        view: new ol.View({
            center: [0, 0],
            zoom: 4,
        })
    });

    thisMap.on('click', function(evt){

    });

    this.maps.push(thisMap);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM