react-native - react-native-maps:animateToRegion 不适用于区域或初始区域?
问题描述
简而言之:为什么 animateToRegion 不适用于 region 或 initialRegion?
我已经测试了这种行为,并注意到 animateToRegion 在 useEffect() => {}, []); 中使用时也不起作用
但是,如果我向 animateToRegion 添加一个带有功能的简单按钮,那么带有 region 或 initialRegion 的 animateToRegion 将起作用。如果我在 MapView 上为 onPress/onLongPress 设置动画,它也可以工作。
以下代码几乎是 MapView 的用法。fitToMarker() 基本上从用户或默认位置获取位置,然后将它们传递给 animateToRegionHolder()。
const mapRef = React.useRef<MapView | null>(null);
const animateToRegionHolder = (lat: number, lng: number) => {
if (mapRef.current) {
mapRef.current.animateToRegion(
{
latitude: lat,
longitude: lng,
latitudeDelta: 0.2,
longitudeDelta: 0.0421,
},
1000,
);
}
};
<MapView
ref={mapRef}
onMapReady={fitToMarker}
style={[S.MapsStyles.mapView, defineMapContainerStyle()]}
/**
* This for some reason affects that fitToMarker will not work
* But removing this will affect slight animation (which is not desired) on initial render
* initialRegion={O.region}
*/
>
<MapView>
“世博会”:“^43.0.0”,
“react-native-maps”:“0.28.0”,
为读者提供的解决方案由于使用with
而出现问题。有条件地使用解决:
需要拖动地图时不使用,需要初始化时使用。onMapReady
initialRegion
onRegionChangeComplete
animateToRegion
解决方案
尝试useRef
改用。这是一个工作示例:
const mapRef = React.useRef<MapView >(null);
<MapView
ref={mapRef}
initialRegion={initialRegion}
onRegionChangeComplete={handleRegionChange}
/>
mapRef.current?.animateToRegion({
latitude: selectedCoordinates.lat,
longitude: selectedCoordinates.lng,
longitudeDelta: 0.004,
latitudeDelta: 0,
});
推荐阅读
- angular - 模板语句中的角度异步参数
- c++ - Segfault 尝试在前向声明的类中使用 shared_ptr
- c++ - 如何确保两个字符串在c ++中只有某些字母
- python - 尝试在不同类的gridlayot上添加按钮时出现kivy错误
- python - Discord python关于角色授予事件
- python - 根据 json 列表字符串检查用户输入
- javascript - Chrome 扩展,将消息从注入脚本发送到后台的最佳方式
- javascript - 将文本附加到单个文本区域 - 按段落分隔(使用 javascript)
- kubernetes - 执行到另一个容器并访问其目录的最佳方法是什么?
- android - 我的 android 应用是否需要使用 Google Play In-app Billing 进行应用内订阅?