【问题标题】:React native maps animateToRegion doesn't work on androidReact 本机地图 animateToRegion 不适用于 android
【发布时间】:2021-07-29 13:04:48
【问题描述】:

今天我遇到了 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,
                    }}

                />
)

}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2014-10-01
      相关资源
      最近更新 更多