简体   繁体   English

react-native-maps 无法在无状态组件中引用 MapView

[英]react-native-maps Cannot ref the MapView in Stateless Component

there are a lot of example on how to use react-native-maps in a Class Component but i haven't found those with Stateless Component.有很多关于如何在类组件中使用 react-native-maps 的示例,但我还没有找到带有无状态组件的示例。 I use Stateless Component to access the a global state.我使用无状态组件来访问全局状态。 I need to move the camera view of the MapView from the initial region after i got the user's location , After searching in internet i somehow ends up with this code that is not working.获得用户位置后,我需要从初始区域移动 MapView 的相机视图,在互联网上搜索后,我不知何故以无法正常工作的代码结束。

import React, { useState, useEffect, useRef } from 'react'
import MapView, { Marker, PROVIDER_GOOGLE } from 'react-native-maps';
...

const Explore = props => {
    const [userCoords, setUserCoords] = useState({
        latitude: 0,
        longitude: 0
    })

    const mapRef = useRef(null);

    onMapReady = () => {
        getUserLocation()
    }

    getUserLocation = async () => {
        Geolocation.getCurrentPosition(
            async (position) => {
                setUserCoords(position.coords)
                mapRef.animateToRegion({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                })
            },
            async (error) => {
                // See error code charts below.
                console.log("Geolocation Error" + error.code, error.message);
            },
            { enableHighAccuracy: true, timeout: 15000 }
        );
    }

    return(
    ...
    <MapView
         ref={mapRef}
         onMapReady={() => onMapReady()}
         region={{
             latitude: userCoords.latitude,
             longitude: userCoords.latitude,
             latitudeDelta: 0.0922,
             longitudeDelta: 0.0421,
             showsUserLocation={true}>
             ...
    </MapView>
    )
...
export default Explore

Please help me~ Thank you请帮帮我~谢谢

而不是使用的mapRef.animateToRegion你需要使用mapRef.current.animateToRegion

there are a lot of example on how to use react-native-maps in a Class Component but i haven't found those with Stateless Component.有很多关于如何在类组件中使用 react-native-maps 的示例,但我还没有找到那些带有无状态组件的示例。 I use Stateless Component to access the a global state.我使用无状态组件来访问全局状态。 I need to move the camera view of the MapView from the initial region after i got the user's location , After searching in internet i somehow ends up with this code that is not working.在获得用户的位置后,我需要从初始区域移动 MapView 的相机视图,在互联网上搜索后,我不知何故以无法正常工作的代码结束。

import React, { useState, useEffect, useRef } from 'react'
import MapView, { Marker, PROVIDER_GOOGLE } from 'react-native-maps';
...

const Explore = props => {
    const [userCoords, setUserCoords] = useState({
        latitude: 0,
        longitude: 0
    })

    const mapRef = useRef(null);

    onMapReady = () => {
        getUserLocation()
    }

    getUserLocation = async () => {
        Geolocation.getCurrentPosition(
            async (position) => {
                setUserCoords(position.coords)
                mapRef.animateToRegion({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                })
            },
            async (error) => {
                // See error code charts below.
                console.log("Geolocation Error" + error.code, error.message);
            },
            { enableHighAccuracy: true, timeout: 15000 }
        );
    }

    return(
    ...
    <MapView
         ref={mapRef}
         onMapReady={() => onMapReady()}
         region={{
             latitude: userCoords.latitude,
             longitude: userCoords.latitude,
             latitudeDelta: 0.0922,
             longitudeDelta: 0.0421,
             showsUserLocation={true}>
             ...
    </MapView>
    )
...
export default Explore

Please help me~ Thank you请帮帮我~谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM