首页 > 解决方案 > watchPosition 的问题反应原生博览会

问题描述

我想在更改位置时发送用户的位置,但这对我不起作用。我曾尝试使用在此平台上找到的其他代码,但我尝试将其转换为spaghetti code。我正在清理它,这是我的代码:

const get_user_id = await AsyncStorage.getItem('user_id');
      this.setState({ 
        user_id: get_user_id,
      });
      navigator.geolocation.watchPosition(
        position => {
          const locationLat = JSON.stringify(position.coords.latitude);
          const locationLong = JSON.stringify(position.coords.longitude);

          this.setState({ 
            lat: locationLat,
            long: locationLong,
          });
          
          fetch(confg.url+'guardaralerta', {
            method: 'POST',
            headers: {
              Accept: 'application/json',
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({
              user_id: this.state.user_id,
              lat: this.state.lat,
              long: this.state.long,
            })
          }).then((response) => response.json())
          .then((responseData) => {
              console.log(JSON.stringify(responseData))  
          });
        },
        error => Alert.alert(error.message),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
      );
  } catch (error) {
    console.log(error)    
  }

标签: react-nativegeolocationlocation

解决方案


推荐阅读