反应原生地图 animateToRegion 在 android 上不起作用

时间:2021-07-29 13:04:48

标签: reactjs react-native react-native-android react-native-ios react-native-maps

今天我遇到了 React Native Maps 的问题。我尝试使用 animateToRegion 在地图上显示标记,在 IOS 上一切正常,但在 android 上它不起作用。我绝对确定问题出在 animateToRegion 上。我已经尝试在没有 useEffect 的情况下仅在单击按钮时使用它,并且每次打开地图时都会显示我的 console.log('AnimateToRegion'),但是在 console.log 之后立即运行的 animateToRegion 在 Android 上不起作用。

这是我的代码:

const Map = ({navigation, route}: HomeNavigationProps<"Map">) => {
    const [destination, setDestination] = useState(undefined);
    const mapView = React.createRef();
    const isFocused = useIsFocused();
   
    useEffect(() => {
        console.log('AnimateToRegion')
        if (route.params || destination){
            if(destination && route.params){
                mapView.current.animateToRegion({ // Takes a region object as parameter
                    latitude: route.params.destination.latitude,
                    longitude: route.params.destination.longitude,
                    latitudeDelta: 0.2,
                    longitudeDelta: 0.4,
                },1000);
            }
            if (route.params && !destination) {
                mapView.current.animateToRegion({ // Takes a region object as parameter
                    latitude: route.params.destination.latitude,
                    longitude: route.params.destination.longitude,
                    latitudeDelta: 0.2,
                    longitudeDelta: 0.4,
                }, 1000);
            }
            if (destination && !route.params) {
                mapView.current.animateToRegion({ // Takes a region object as parameter
                    latitude: destination.latitude,
                    longitude: destination.longitude,
                    latitudeDelta: 0.2,
                    longitudeDelta: 0.4,
                }, 1000);
            }
        }
       
    }, [isFocused]);
return (
          <MapView
                    ref={mapView}
                    mapType={satellite ? "hybrid" : "standard"}
                    style={{flex: 1}}
                    showsUserLocation={true}
                    followsUserLocation={true}
                    provider={PROVIDER_GOOGLE}
                    initialRegion={{

                                        latitude: 53.227200,
                                        longitude: 50.243698,
                                        latitudeDelta: 3,
                                        longitudeDelta: 3,
                    }}

                />
)

}

1 个答案:

答案 0 :(得分:0)

我终于知道是怎么回事了!问题是我使用字符串表示经度/纬度,不知何故 IOS 可以解析字符串,但 android 不能。我认为在这种情况下应该发出警告。