找到你要的答案

Q:OpenLayers map.events.register throws TypeError

Q:OpenLayers map.events.register抛出TypeError

I need to see the event of my map in OpenLayers 3 but my JavaScript doesn't work.

This is my code:

var map = new ol.Map({
  layers: [raster, vector],
  target: 'map',
  view: new ol.View({
            projection: 'EPSG:900913',
            center: [-110000, 4600000],
            zoom:5
        })
});
 map.events.register("zoomend", this, function (e) {
    if (map.getZoom() === this.mapMaxZoom) {
        // Don't cluster at this level. No matter what.
        this.vector.strategies[0].threshold = 1000;
        console.log("setting the clustering strategy to 1000");
    }
});

And the console output:

Uncaught TypeError: Cannot read property 'register' of undefined

Please help me.

我需要看到我的地图在OpenLayers 3我的JavaScript不工作时。

这是我的密码:

var map = new ol.Map({
  layers: [raster, vector],
  target: 'map',
  view: new ol.View({
            projection: 'EPSG:900913',
            center: [-110000, 4600000],
            zoom:5
        })
});
 map.events.register("zoomend", this, function (e) {
    if (map.getZoom() === this.mapMaxZoom) {
        // Don't cluster at this level. No matter what.
        this.vector.strategies[0].threshold = 1000;
        console.log("setting the clustering strategy to 1000");
    }
});

和控制台输出:

Uncaught TypeError: Cannot read property 'register' of undefined

请帮助我。

answer1: 回答1:

Try using map.on() with the 'moveend' event as follows:

map.on('moveend', function(e) {...});

See the following for a complete example:

http://openlayers.org/en/v3.9.0/examples/moveend.html

尝试使用地图。on()的MoveEnd事件如下:

map.on('moveend', function(e) {...});

见下面的一个完整的例子:

http://openlayers.org/en/v3.9.0/examples/moveend.html

javascript  openlayers-3