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().

Name Type Description
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)

中心點坐標就獲取到了

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM