Javascript 将事件绑定到openlayer3映射

Javascript 将事件绑定到openlayer3映射,javascript,openlayers-3,Javascript,Openlayers 3,我的网页中有一个div,它包含许多选项卡,每个选项卡中都包含一个openlayer地图 问题是,当我使用以下代码将单击事件附加到地图时,它不起作用 //create map in given div ID// createMap : function(id) { var _this = this;//To refer this dojo AMD module //Array of maps. //_this.index to tra

我的网页中有一个div,它包含许多选项卡,每个选项卡中都包含一个openlayer地图

问题是,当我使用以下代码将单击事件附加到地图时,它不起作用

//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);
}
试着这样做:

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