简体   繁体   English

将事件绑定到openlayer3映射

[英]Binding event to a openlayer3 map

I have a div in my webpage that contains many tabs and each tab contains one openlayer map in it. 我的网页中有一个div,其中包含许多标签,每个标签中都包含一个openlayer映射。

The problem is when i'm using following code to attach a click event to the map , it is not working. 问题是当我使用以下代码将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);
        });
    }

But when i'm using the following code to attach event its working fine. 但是当我使用以下代码附加事件时,它的工作正常。

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

But i don't want to use the second method to register the event because the first approach is a part of openlayer and i can do so many things with the mouseEvent argument like i can get the latitude and longitude directly. 但是我不想使用第二种方法注册事件,因为第一种方法是openlayer的一部分,我可以使用mouseEvent参数做很多事情,例如我可以直接获取经度和纬度。

Earlier both were used to working but then i re-created my GUI using dojo widget after that first method stopped working. 早先两个都可以正常工作,但是在第一种方法停止工作后,我使用dojo小部件重新创建了GUI。

Can anyone help me what's wrong ? 谁能帮我什么忙? or what additional thing require to use first method? 或使用第一种方法还需要什么其他东西?

Try this way: 尝试这种方式:

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