Open Layers基础知识
部分内容来自
地图组件Map
要素检测
Map 提供方法来检测给定像素处的要素:
// 检查像素处是否有要素
const hasFeature = map.hasFeatureAtPixel(pixel, options);
// 获取像素处的所有要素
const features = map.getFeaturesAtPixel(pixel, options);
// 对像素处的每个要素执行回调
map.forEachFeatureAtPixel(pixel, function(feature, layer) {
// 对要素执行操作
}, options);
这些方法对于实现要素选择和交互很有用。
坐标转换方法
// 从像素转换为坐标(用户投影)
const coordinate = map.getCoordinateFromPixel(pixel);
// 从坐标转换为像素
const pixel = map.getPixelFromCoordinate(coordinate);
// 从鼠标/触摸事件获取坐标
const eventCoord = map.getEventCoordinate(event);
// 内部方法(视图投影)
const viewCoord = map.getCoordinateFromPixelInternal(pixel);
const viewPixel = map.getPixelFromCoordinateInternal(coordinate);
视图系统View
常见配置
const view = new View({
center: [0, 0], // 初始中心点
zoom: 2, // 初始缩放级别(替代分辨率)
rotation: 0, // 初始旋转角度(弧度)
projection: 'EPSG:3857', // 地图投影
extent: [-180, -90, 180, 90], // 约束范围
constrainResolution: true, // 交互后吸附到最近的缩放级别
maxZoom: 18, // 最大缩放级别
minZoom: 0 // 最小缩放级别
});