mapbox-gl中的web墨卡托轉(zhuǎn)換
mapbox-gl的api開放了web墨卡托和經(jīng)緯度的轉(zhuǎn)換,直接使用mapbox-gl的api能夠進(jìn)行web墨卡托的坐標(biāo)操作。
api說明網(wǎng)址:https://docs.mapbox.com/mapbox-gl-js/api/geography/#mercatorcoordinateweb
墨卡托的坐標(biāo)說明:http://epsg.io/3857
?將經(jīng)緯度坐標(biāo)轉(zhuǎn)換成web墨卡托坐標(biāo),包含了高度的信息:
?let?modelAsMercatorCoordinate?=?mapboxgl.MercatorCoordinate.fromLngLat(?[117,36],
? modelAltitude);?
其中的web墨卡托坐標(biāo)獲?。?/p>
let xcoord=modelAsMercatorCoordinate.x,
let ycoord=modelAsMercatorCoordinate.y,
let zcoord=modelAsMercatorCoordinate.z,
上述坐標(biāo)的結(jié)果,并不是[-20026376.39 -20048966.10?20026376.39 20048966.10]范圍內(nèi)的坐標(biāo)信息,需要根據(jù)api再進(jìn)行一次轉(zhuǎn)換,使用下列的方法,轉(zhuǎn)換成米制的單位:
let meterunit = modelAsMercatorCoordinate.meterInMercatorCoordinateUnits()
xcoord/meterunit , ycoord/meterunit ?即獲取的是上述范圍中的坐標(biāo)。
初始化一個web墨卡托的坐標(biāo)信息,x\y\z為坐標(biāo)信息:
let?mecatorcoord?=?new?mapboxgl.MercatorCoordinate(x,y,z);
web墨卡托坐標(biāo)轉(zhuǎn)換成經(jīng)緯度坐標(biāo):
??let?datalngLat?=?mecatorcoord.toLngLat();
獲得坐標(biāo)的高度值:let?datalngLat?=?mecatorcoord.toAltitude();
mapbox-gl中的web墨卡托坐標(biāo),是不依賴于地圖初始化操作的,單獨就能夠進(jìn)行調(diào)用、使用,直接或者node形式加載都是可以的。