【问题标题】:Wrong Geolocation with React Native and iOS simulatorReact Native 和 iOS 模拟器错误的地理位置
【发布时间】:2017-08-07 20:40:36
【问题描述】:

我正在使用 react-native-maps 并使用 react-native api 进行地理定位。当我通过 api 使用位置时,模拟器上的返回显示我在旧金山而不是我所在的科罗拉多州丹佛市。我的位置无法显示我所在的位置是否有原因?

我的运行位置的函数

  // Get current users locaiton on load
  componentDidMount() {
    navigator.geolocation.getCurrentPosition((position) => {
      console.log(position);
      let newLat = parseFloat(position.coords.latitude);
      console.log("newLat", newLat);
      let newLng = parseFloat(position.coords.longitude);
      console.log("newLng", newLng);
      // set region of user to lat and long position
      // deltas set distance from marker (zoom) on map
      this.setState({
        region: {
          latitude: newLat,
          longitude: newLng,
          latitudeDelta: 0.0250,
          longitudeDelta: 0.0125
        },
        error: null
      });
      console.log(this.state);
    }, 
      // if error set state with error
      (err) => {
        console.log(err),
        // set location accuracy on
        // max time to find location in 5 sec
        // max time to use cached location for phone 5 sec
        { timeout: 5000, maximumAge: 5000 };
      }
    );

    // watch for changes and update location
    this.watchId = navigator.geolocation.watchPosition((position) => {
      console.log(position);
      let newLat = parseFloat(position.coords.latitude);
      console.log("newLat", newLat);
      let newLng = parseFloat(position.coords.longitude);
      console.log("newLng", newLng);
      // set region of user to lat and long position
      // deltas set distance from marker (zoom) on map
      this.setState({
        region: {
          latitude: newLat,
          longitude: newLng,
          latitudeDelta: 0.0250,
          longitudeDelta: 0.0125
        },
        error: null
      });
      console.log(this.state);
    },
      (err) => {
        console.log(err),
        // set location accuracy on
        // max time to find location in 5 sec
        // max time to use cached location for phone 5 sec
        // distance before location called again 10 m
        { timeout: 5000, maximumAge: 5000, distanceFilter: 10 }
      }
    );
  }

我在初始状态下预设的位置是:

this.state ={
      region: {
          latitude: 39.739236,
          longitude: -104.990251,
          latitudeDelta: 0.1000,
          longitudeDelta: 0.0500
      },

这比 iOS 模拟器显示的位置更接近

有人遇到这个问题并知道如何解决吗?是 react-native 正在运行的网络还是不应该从我的笔记本电脑网络位置提取?

我已查看是否有其他人遇到此问题: Geolocation in React Native iOS 如果您搜索地理位置错误的 react-native ios,则堆栈上的搜索仅显示 6 个结果...google 也没有太大帮助。

感谢您的帮助

【问题讨论】:

    标签: ios react-native ios-simulator apple-maps react-native-maps


    【解决方案1】:

    这是 iOS 模拟器(和 Android 模拟器)上的正确行为。您可以像这样更改每个地理定位的模拟坐标(从其他答案转述):

    iOS

    1. 在 iOS 模拟器中运行应用程序。
    2. 在顶部菜单栏中,您会找到功能 -> 位置 -> 自定义位置..(或者您可以选择使用其他位置)。
    3. 设置位置的纬度和经度。

    Android

    1. 在 Android 模拟器中运行应用程序。
    2. 单击工具栏中的省略号 (...)。
    3. 在位置下新打开的设置中编辑坐标。

    还有一个用于 Android explained here 的命令行替代方案,使用 telnetgeo fix 命令。

    或者,您可以按照 ashutosh pandey 的建议在真实设备上进行测试。

    【讨论】:

    • 有趣,感谢您告诉我如何解决此问题,以便我可以测试我的地理位置!
    • 这不适用于 ios。无论如何,我仍然可以获得当前位置。
    • @Goehybrid 重启模拟器查看修改后的位置。
    最近更新 更多