【问题标题】:react native geolocation not providing exact location in real device反应原生地理位置未在真实设备中提供确切位置
【发布时间】:2018-02-16 05:58:31
【问题描述】:


您是否在 react native 中做过像 Uber 这样的实时跟踪演示?

我做了一个。它在具有自定义位置的模拟器和模拟器中运行良好,但无法使用 navigator.geolocation 在真实设备中获得正确的位置。

watchID = null;

componentDidMount(){


    navigator.geolocation.getCurrentPosition((position) => {
        let lat = parseFloat(position.coords.latitude);
        let long = parseFloat(position.coords.longitude);

        let initialRegion = {
            latitude : lat,
            longitude : long,
            latitudeDelta : LATITUDE_DELTA,
            longitudeDelta : LONGITUDE_DELTA
        }

        this.setState({ initialPosition : initialRegion });
        this.setState({ markerPostition : initialRegion });
    }, (error) => { 
        alert(JSON.stringify(error)) 
    });


    this.watchID = navigator.geolocation.watchPosition((position) => {

         let lat = parseFloat(position.coords.latitude);
         let long = parseFloat(position.coords.longitude);

        let lastRegion = {
            latitude : lat,
            longitude : long,
            latitudeDelta : LATITUDE_DELTA,
            longitudeDelta : LONGITUDE_DELTA
        }

        this.setState({ initialPosition : lastRegion });
        this.setState({ markerPostition : lastRegion });    
    });


}

componentWillUnmount(){
    navigator.geolocation.clearWatch(this.watchID);
}

我已将 enableHighAccuracy 设置为 true,但它抛出错误,所以,我只是将其删除。现在我没有得到设备的正确(准确)位置。

请建议我使用 watchPosition 进行地理定位设置,以防您进行任何相关项目时对您更有效。

如果你在 git repo 中有你的代码,那么我会很感激你的。 如果我犯了任何语法错误,请原谅我。

【问题讨论】:

    标签: reactjs react-native geolocation tracking


    【解决方案1】:
    componentDidMount() {
      Geolocation.getCurrentPosition(
        (pos) => {
          const coords = {
            latitude: pos.coords.latitude,
            longitude: pos.coords.longitude
          };
          this.setState((prevState) => {
            return {
              focusedLocation: {
                ...prevState.focusedLocation,
                latitude: coords.latitude,
                longitude: coords.longitude
              }
            };
          });
          console.log(coords.latitude, coords.longitude);
          return this.geocoder(coords.latitude, coords.longitude);
        },
        (err) => {
          alert('Fetching the Position failed, please check location is enable!');
        },
        { enableHighAccuracy: false, timeout: 10000 }
      );
    }
    
    

    这将完美地工作......试试这个

    【讨论】:

    • 在第一行的代码中添加代码格式并考虑添加一些解释
    【解决方案2】:

    我在我的项目中使用了地理定位,它运行良好,这是我的地理定位设置尝试,如果它解决了您的问题

     navigator.geolocation.getCurrentPosition(
                (position) => {
                  //position.coords.latitude for latitude 
                  //position.coords.longitude for longitude
                },
                (error) => console.warn(error.message),
                { enableHighAccuracy: false, timeout: 10000 }
              )
    

    观察位置代码和你的一样

    【讨论】:

    • 感谢 Shashank Srivastava,我通过在 getCurrentPosition 中添加 {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000} 并添加 { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, 解决了我的问题distanceFilter: 10 } 在 watchPosition 谢谢
    【解决方案3】:

    启用高精度:真 这将有助于找到确切的位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-28
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      相关资源
      最近更新 更多