React Native MapViewDirections:强制整个路线保持在视图中?

问题描述 投票:0回答:1

我正在遵循 this 指南,在 React Native 中使用

MapViewDirections
在地图上的两点之间绘制路线。我可以通过根据起点和终点的坐标指定
latitudeDelta
longitudeDelta
来将起点和终点保留在视图内。但是,如果 Google 地图计算的路线蜿蜒到一边,我最终可能会遇到这样的情况:起点和终点都在视图内,但路线不在视图内

我想知道的是:有没有办法让视图自动缩放以适应整个路线?我的代码类似于教程

<MapViewDireactions
  origin={this.state.coordinates[0]}
  destination={this.state.coordinates[1]}
  apikey={GOOGLE_MAPS_APIKEY}
/>
reactjs react-native google-maps google-maps-api-3 react-native-maps
1个回答
0
投票

U 应该使用

useEffect
来实现这一点。我在这里编写了如果出发地或目的地发生更改则执行的代码:

  useEffect(() => {
if (!origin || !destination) return;

mapRef.current.fitToSuppliedMarkers(["origin", "destination"], {
  edgePadding: { top: 50, right: 50, bottom: 50, left: 50 },
}); }, [origin, destination]);

您应该像

mapRef
一样创建
  const mapRef = useRef(null);
。 另外“原点”是我的起点标记名称。 “目的地”是我的终点标记名称。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.