2017-09-01 150 views
0

我已經在所有fitbounds教程Mapbox跑了,但還是無法弄清楚如何重新調整我的地圖給定的光柵層上。我有一個菜單,用於切換一些較舊的地圖,並且希望在每個轉彎處重新調整地圖。擬合的地圖範圍光柵層

這是我到目前爲止。 mapnumber是我的柵格地圖的字符串。

 var coordinates = map.getBounds(mapnumber); 
     var bounds = new mapboxgl.LngLatBounds(coordinates[0], coordinates[0]); 
     map.fitBounds({bounds}); 

的界限在那裏,我不能對他們使用fitbounds。這是錯誤。

lng_lat.js:97 Uncaught Error: `LngLatLike` argument must be specified 
as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array 
of [<lng>, <lat>] 
+0

嘗試'map.fitBounds(邊界);'沒有括號{} – xmojmr

+0

不,這也不行。 – east1999

回答

0

Uncaught Error: LngLatLike argument must be specified as a LngLat instance

它看起來像你使用mapboxgl.LngLatBounds不正確。您需要傳入兩個LngLat實例,而不是兩個座標。該API docs有你的邊界框的西南角和東北角創建點的例子:

var sw = new mapboxgl.LngLat(-73.9876, 40.7661); 
var ne = new mapboxgl.LngLat(-73.9397, 40.8002); 
var llb = new mapboxgl.LngLatBounds(sw, ne); 

然後你可以使用map.fitBounds()與邊框

map.fitBounds(llb); 

UPDATE:回答跟進問題

how can I extract the two sets (sw and ne) from the raster?

可以使用map.getSource() method。傳入柵格圖層的源ID,並返回一個具有bounds屬性的對象。使用此邊界屬性傳入map.fitBounds()

var bounds = map.getSource('mapbox://username.source-id').bounds; 
map.fitBounds(bounds); 

這裏有一個工作示例:https://codepen.io/mapsam/pen/RZvyBQ

+0

謝謝!但是,我也認爲是這種情況,如何從光柵中提取兩組(sw和ne)?我知道它有它們,但使用'coordinate [0]'不起作用。柵格文件由Mapbox託管。 – east1999

+0

@ east1999很好的問題!我已經更新了上面的答案,指出如何使用map.getSource()來檢索柵格圖層的邊界。 – mapsam

+0

回想起來,我只是想感謝您的回答,以及您對Mapbox的驚人和鼓舞人心的工作。作爲參考,儘管我將問題標記爲已解決,但這有點複雜。 mapname是使用JavaScript函數動態地拉動的(需要用它來構建菜單),所以我不確定如何將它與getSource結合起來。我可以使用該圖層的名稱來獲取其源代碼ID嗎?旁邊的問題,但再次,謝謝你! – east1999