Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

Update react-native-maps on React Navigation

I'm building an app with react native and Expo, using react native maps, and also react navigation V5 for navigating between screens.

When I update a state, I use setState, since I'm using a class, and the App renders, so far so good.

I use location in the maps, so I have to wait for permission to get the location of the users, by using the function: componentDidMount()

Problems ahead, now when I update a state in componentDidMount(), (it means, when the location is done) it renders in the App, but not the screen, but my map is in the initial screen, and it does not receive any updates.

this is how I proceed :

    export default class App extends React.Component {
      constructor() {
        super()
        this.state = {
          initialPosition: {
            latitude: 0,
            longitude: 0,
            latitudeDelta: 0,
            longitudeDelta: 0,
          },      
          initialMarker: {
            latitude: 0,
            longitude: 0,
          }     
        }
      }
    
      
      componentDidMount(){
        this.getLocationAsync();    
      }
      
      
      getLocationAsync = async () => {
        let { status } = await Permissions.askAsync(Permissions.LOCATION);
        if (status !== 'granted') {
          this.setState({
            errorMessage: 'Permission to access location was denied',
          });
        }
    
        let location = await Location.getCurrentPositionAsync({accuracy:Location.Accuracy.BestForNavigation});
        const { latitude , longitude } = location.coords
        this.getGeocodeAsync({latitude, longitude})
       
       this.setState({ initialPosition: {
            latitude: latitude,
            longitude: longitude,
            latitudeDelta: 0,
            longitudeDelta: 0,
          }});    
          
          this.setState({ initialMarker: {
            latitude: latitude,
            longitude: longitude,
          }});
        
    
        console.log('GOT LOCATION')
    
      };

the App render part:

render(){ 
  return (
    <NavigationContainer>
      <Drawer.Navigator initialRouteName="Home">
        <Drawer.Screen name="HISTORIQUE" component={this.HomeScreen}/>
        <Drawer.Screen name="MOYENS DE PAYEMENT" component={this.NotificationsScreen} />
        <Drawer.Screen name="PARRAINAGE" component={this.NotificationsScreen} />
        <Drawer.Screen name="CONSTACTER PASS" component={this.NotificationsScreen} />
        <Drawer.Screen name="PARAMèTRES" component={this.NotificationsScreen} />
        <Drawer.Screen name="DéCONNEXION" component={this.NotificationsScreen} />
      </Drawer.Navigator>
    </NavigationContainer>
    
    );   
 }

I use : this.state.initialPosition, in the screen, this works at first load, but when the componentDidMount() is donne and update the state, it doesn't update in the screen.

the Home screen used for maps:

HomeScreen =({ navigation}) => {

     return (
        <View style={styles.container}>
          <MapView
            style={styles.map}
            initialRegion={this.state.initialPosition}           
            region={this.state.initialPosition}>
                
                <Marker
                    coordinate={this.state.initialMarker}
                    title={"Your location"}
                >   

                </Marker>
        
            </MapView>
        </View>
    );
}

Current behavior : Marker and map position : is zero, or at the middle of the ocean.

Wanted behavior : Marker and map position: at my current location.

My question is: How can I update the position of the map, after location data is done ?

In another way : How can I update a variable inside a screen after componentDidMount() function is done ?

question from:https://stackoverflow.com/questions/65623338/update-react-native-maps-on-react-navigation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem solved !

  1. Use a class instead of a function:

    class HomeScreen extends React.Component { ... }

    instead of:

    HomeScreen =({ navigation}) => {...}

  2. Put the state and componentDidMount() function inside the HomeScreen class.

  3. Call the component as class not a function:

    <Drawer.Screen name="HOME" component={HomeScreen}/>

  4. To use the navigation object : set it in the render() of the HomeScreen class:

    const { navigation } = this.props;

check React Navigation docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...