admin管理员组文章数量:1332339
I am using Mapbox-gl package for Mapbox. I am rendering the map inside the useEffect and what I want to do is change the centre of Mapbox without pletely rerendering the map. for example
const mapContainerRef = useRef(null);
const [markerLngLat, setMarkerLngLat] = useState([85.324, 27.7172]);
useEffect(() => {
const map = new mapboxgl.Map({
container: mapContainerRef.current,
style: 'mapbox://styles/lmaps/ckl6t1boq578819qod5v7ynby',
center: markerLngLat,
zoom: 13,
});
}, []);
return (
<div>
<a onClick={setMarkerLngLat([65.468754, 44.57875])} />
<div className='listing-map-container' ref={mapContainerRef} />
</div>
);
With the click of the button, I want to move the centre of the map from the previous lat long to the newly set Latlong without re-rendering the entire map. Passing markerLngLat in the [] in useEffect works but it pletely re-rerenders the map and all other 1000's markers on it, so can't prefer that way. The actual code is much longer and many markers are being marked on the map so I don't want to re-render the map entirely.
I am using Mapbox-gl package for Mapbox. I am rendering the map inside the useEffect and what I want to do is change the centre of Mapbox without pletely rerendering the map. for example
const mapContainerRef = useRef(null);
const [markerLngLat, setMarkerLngLat] = useState([85.324, 27.7172]);
useEffect(() => {
const map = new mapboxgl.Map({
container: mapContainerRef.current,
style: 'mapbox://styles/lmaps/ckl6t1boq578819qod5v7ynby',
center: markerLngLat,
zoom: 13,
});
}, []);
return (
<div>
<a onClick={setMarkerLngLat([65.468754, 44.57875])} />
<div className='listing-map-container' ref={mapContainerRef} />
</div>
);
With the click of the button, I want to move the centre of the map from the previous lat long to the newly set Latlong without re-rendering the entire map. Passing markerLngLat in the [] in useEffect works but it pletely re-rerenders the map and all other 1000's markers on it, so can't prefer that way. The actual code is much longer and many markers are being marked on the map so I don't want to re-render the map entirely.
Share Improve this question edited Jan 11, 2022 at 7:50 AdriSolid 2,8251 gold badge10 silver badges18 bronze badges asked Mar 5, 2021 at 18:15 Ankit ShahAnkit Shah 1,3452 gold badges18 silver badges34 bronze badges1 Answer
Reset to default 5Your are re-creating the Mapbox instance each time you set a new state, try using some method like setCenter
or flyTo
, directly to the instance, something like:
const mapRef = useRef();
const [mapObject, setMap] = useState();
useEffect(() => {
const map = new mapboxgl.Map({
container: mapContainerRef.current,
style: 'mapbox://styles/lmaps/ckl6t1boq578819qod5v7ynby',
center: markerLngLat,
zoom: 13,
});
setMap(map);
},[]);
function setMapCenter(coords) {
if (mapObject) {
mapObject.setCenter(coords);
}
}
return (
<div>
<a onClick={() => setMapCenter([65.468754, 44.57875])} />
<div className='listing-map-container' ref={mapRef}></div>
</div>
);
本文标签: javascriptHow to change center Latitude and Longitude of a mapboxgl in react jsStack Overflow
版权声明:本文标题:javascript - How to change center Latitude and Longitude of a mapbox-gl in react js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742330152a2454529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论