openlayers 注册地图事件(移动、单击、拖动)

1、创建一个地图对象

// Layers
    var layers = [
        new ol.layer.Tile({
            name: "Natural Earth",
            minResolution: 306,
            source: new ol.source.XYZ(
                {
                    url: 'https://{a-d}.tiles.mapbox.com/v3/mapbox.natural-earth-hypso-bathy/{z}/{x}/{y}.png',
                    attributions: ['&copy; <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ']
                })
        })
    ]
    // The map
    var map = new ol.Map
    ({
        target: 'map',
        view: new ol.View
        ({
            zoom: 5,
            center: [261720, 5951081]
        }),
        controls: ol.control.defaults({"attribution": false}),
        layers: layers
    });

2、加入所需的事件,事件中即可进行操作

// 鼠标移动事件
map.on('pointermove', function (evt) {
    // 经纬度坐标
    var coord = ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326');
});

// 地图拖动事件
map.on("moveend", function (evt) {
    
});

// 单击地图事件
map.on("singleclick", function (evt) {
    
});

 

转载自:https://blog.csdn.net/liuxin00020/article/details/81946037

You may also like...