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: ['© <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) {
});