openlayer 5 获取中心点

根据openlayer5中方法map.getView().getCenter()同样可以获取中心点坐标。

------------------------------------------------------------------------------------------------------------------

之前在控制台把map对象打印出来后,有一个很像中心点坐标map.focus_。我可以确定的告诉大家,这个不是中心点坐标,检查出来的结果是地图中心最右的点位。

在创建地图时,会将中心点左边写在view中,那么获取中心点坐标也是在view中

在官网中View中有一个事件--calculateExtent(opt_size)官网中是这么介绍的

Calculate the extent for the current view state and the passed size. The size is the pixel dimensions of the box into which the calculated extent should fit. In most cases you want to get the extent of the entire map, that is map.getSize().

NameTypeDescription
size     module:ol/size~Size

Box pixel size. If not provided, the size of the first map that uses this view will be used.(框像素大小。如果未提供,将使用使用此视图的第一个地图的大小。

翻译过来就是:

计算当前视图状态和传递大小的范围。大小是计算范围应适合的框的像素尺寸。在大多数情况下,您希望获得整个地图的范围,即map.getSize()

把这个方法结果打印出来之后发现是一个四位数组:代表的是“框像素大小。如果未提供,将使用使用此视图的第一个地图的大小。”

在openlayer中还能extent查询到方法“getCenter”,那么把两个结果放在一起会有什么化学反应呢?

mapExtent = map.getView().calculateExtent(map.getSize())
map_center = ol.extent.getCenter(mapExtent)

中心点坐标就获取到了

--------------------------------------------------------------------------------------------------------------------------------------------

 

原文地址:https://www.cnblogs.com/lucio110/p/11227414.html