React Mapbox-GL:无法将3D地形添加到地图中的Mapbox API中
我正在使用 react-mapbox-gl 在我的下一个应用程序中,根据文档:
OnstyleLoad :( map,loadevent)=>用MAPBOX地图调用void 实例何时发射加载事件。您可以使用回调的 然后与Mapbox API交互的第一个参数。
这揭示了一个道具,该道具允许一个人直接与mapbox API进行交互,并且在添加a Geocoder 和地理位置控制,但对添加3D地形。
这是我的地图组件:
import { useCallback, useEffect, useRef } from 'react'
import ReactMapboxGl, { RotationControl, ZoomControl } from "react-mapbox-gl";
import mapboxgl from 'mapbox-gl';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
const mapAccess = {
mapboxApiAccessToken: process.env.MAPBOX_ACCESS_TOKEN
}
const Map = ReactMapboxGl({
accessToken: process.env.MAPBOX_ACCESS_TOKEN,
logoPosition: 'bottom-right',
pitchWithRotate: true
});
export default function MyMap() {
return (
<>
<Map
style="mapbox://styles/mapbox/streets-v9"
onStyleLoad={(map) => {
map.addSource('mapbox-dem', {
'type': 'raster-dem',
'url': 'mapbox://mapbox.mapbox-terrain-dem-v1',
'tileSize': 512,
'maxzoom': 14
});
map.setTerrain({ 'source': 'mapbox-dem', 'exaggeration': 1.5 });
map.addLayer({
'id': 'sky',
'type': 'sky',
'paint': {
'sky-type': 'atmosphere',
'sky-atmosphere-sun': [0.0, 0.0],
'sky-atmosphere-sun-intensity': 15
}
}); map.on('load', () => {
console.log('load')
});
map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
position: 'top-right'
})
);
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true,
showUserHeading: true,
showAccuracyCircle: true,
position: 'top-left'
})
);
}}
>
<RotationControl className="mt-12" position="top-right" />
<ZoomControl className="mt-[2.375rem]" position="top-left" />
</Map>
</>
)
}
知道为什么这不起作用吗?
提前致谢!
I am using react-mapbox-gl in my next application, according to the documentation:
onStyleLoad: (map, loadEvent) => void called with the Mapbox Map
instance when the load event is fired. You can use the callback's
first argument to then interact with the Mapbox API.
This exposes a prop which allows one to interact with Mapbox API directly, and that has worked in regards to adding a Geocoder and GeoLocation Control but does not work in regards to adding 3d terrain.
This is my map component:
import { useCallback, useEffect, useRef } from 'react'
import ReactMapboxGl, { RotationControl, ZoomControl } from "react-mapbox-gl";
import mapboxgl from 'mapbox-gl';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
const mapAccess = {
mapboxApiAccessToken: process.env.MAPBOX_ACCESS_TOKEN
}
const Map = ReactMapboxGl({
accessToken: process.env.MAPBOX_ACCESS_TOKEN,
logoPosition: 'bottom-right',
pitchWithRotate: true
});
export default function MyMap() {
return (
<>
<Map
style="mapbox://styles/mapbox/streets-v9"
onStyleLoad={(map) => {
map.addSource('mapbox-dem', {
'type': 'raster-dem',
'url': 'mapbox://mapbox.mapbox-terrain-dem-v1',
'tileSize': 512,
'maxzoom': 14
});
map.setTerrain({ 'source': 'mapbox-dem', 'exaggeration': 1.5 });
map.addLayer({
'id': 'sky',
'type': 'sky',
'paint': {
'sky-type': 'atmosphere',
'sky-atmosphere-sun': [0.0, 0.0],
'sky-atmosphere-sun-intensity': 15
}
}); map.on('load', () => {
console.log('load')
});
map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
position: 'top-right'
})
);
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true,
showUserHeading: true,
showAccuracyCircle: true,
position: 'top-left'
})
);
}}
>
<RotationControl className="mt-12" position="top-right" />
<ZoomControl className="mt-[2.375rem]" position="top-left" />
</Map>
</>
)
}
Any idea why this is not working?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,回复很晚,可以使用
htttps: //github.com/alex3165/reeact-mapbox-gl/issues/79#issuecomment-267913558
sorry for the late reply, this can be achieved using the
https://github.com/alex3165/react-mapbox-gl/issues/79#issuecomment-267913558