【问题标题】:Getting set state of latitude and longitude in React Native在 React Native 中获取经纬度的设置状态
【发布时间】:2021-07-22 12:09:01
【问题描述】:

我正在创建一个需要获取用户纬度和经度的应用。

我正在使用 react-native-geolocation-services 来获取信息。

这是我的代码。

 componentDidMount() {
    hasLocationPermission().then(result => {
      this.setState({_isLocationEnabled: result});
    });
    if (hasLocationPermission) {
      Geolocation.getCurrentPosition(
        position => {
          this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
          });
          console.log(this.state.latitude);
          console.log(this.state.longitude);
        },
        error => {
          console.log(error.code, error.message);
        },
        {
          enableHighAccuracy: true,
          timeout: 15000,
          maximumAge: 10000,
        },
      );
    }
    const fetch = require('node-fetch');
    console.log(this.state.latitude);
    fetch(
      'https://api.weatherapi.com/v1/forecast.json?q=' +
        this.state.latitude +
        ',' +
        this.state.longitude,
      {
        method: 'GET',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
        },
      },
    )
      .then(response => response.json())
      .then(responseJson => {
        console.log(responseJson);
        this.setState({
          isLoading: false,
          dataSource: responseJson,
        });
      .catch(error => {
        console.error(error);
      });
  }

从我的代码中可以看出,如果设备具有位置权限,那么我将获得地理位置和当前位置。在此之后,我设置了纬度和经度的状态。当我打印它们时,它们会被发送到控制台。

当我尝试通过添加 this.state.latitude 在我的 fetch url 中使用它们时,它们是未定义的。为什么是这样?我做错了什么?

【问题讨论】:

    标签: javascript react-native geolocation


    【解决方案1】:

    问题在于Geolocation.getCurrentPosition 是异步的,因此当您收到位置时,执行获取的代码已经执行。

    为了解决这个问题,您需要执行以下操作:

    componentDidMount() {
    hasLocationPermission().then(result => {
      this.setState({_isLocationEnabled: result});
    });
    if (hasLocationPermission) {
      Geolocation.getCurrentPosition(
        position => {
          this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
          }, () => {
            const fetch = require('node-fetch');
            console.log(this.state.latitude);
            fetch(
              'https://api.weatherapi.com/v1/forecast.json?q=' +
                this.state.latitude +
                ',' +
                this.state.longitude,
              {
                method: 'GET',
                headers: {
                  Accept: 'application/json',
                  'Content-Type': 'application/json',
                },
              },
            )
              .then(response => response.json())
              .then(responseJson => {
                console.log(responseJson);
                this.setState({
                  isLoading: false,
                  dataSource: responseJson,
                });
              })
              .catch(error => {
                console.error(error);
              });
          });
          console.log(this.state.latitude);
          console.log(this.state.longitude);
        },
        error => {
          console.log(error.code, error.message);
        },
        {
          enableHighAccuracy: true,
          timeout: 15000,
          maximumAge: 10000,
        },
      );
    }
    

    基本上你需要在setState 完成后执行获取,这样你才能读取更新的状态。

    【讨论】:

    • 我会试试看
    • 这解决了问题,感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 2019-09-12
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2020-11-18
    • 2012-11-08
    相关资源
    最近更新 更多