使用React Native和iOS模拟器进行错误的地理定位

我正在使用react-native-maps并使用react-native api进行地理定位。 当我通过api使用位置时,在模拟器上返回的内容显示我在旧金山不在我所在的科罗拉多州丹佛市。 有没有理由为什么我的位置不会显示我在哪里?

我运行该位置的function

// 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 iOS中的地理定位和堆栈搜索只显示6个结果,如果你搜索geolocation错误的反应原生ios … google也没什么帮助。

谢谢你的帮助

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

iOS版

  1. 在iOS模拟器中运行应用程序。
  2. 在顶部菜单栏中,您将找到Debug – > Location – > Custom Location ..(或者您可以选择使用其他人)。
  3. 设置位置的纬度和经度。

Android的

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

还有一个Android命令行替代方案,使用telnetgeo fix命令解释。

或者,你可以像ashutosh pandey建议并在真实设备上进行测试。

地理位置不适用于Android和ios的模拟器。 如果你在设备上检查它真的可以正常工作。