React Native和iOS模拟器的地理位置错误 [英] Wrong Geolocation with React Native and iOS simulator
问题描述
我正在使用react-native-maps,并使用react-native api进行地理位置定位。当我通过api使用位置信息时,模拟器返回的信息显示我在旧金山,而不是我所在的科罗拉多州丹佛。为什么我的位置无法显示我在哪里?
I am working with react-native-maps and using the react-native api for geolocation. When I am using location through the api what comes back on the simulator shows me in San Francisco not in Denver, CO where I am located. Is there a reason as to why my location would not show where I am at?
运行该位置的函数
// 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 }
}
);
}
我在初始状态下预设的位置是:
my location preset in initial state is :
this.state ={
region: {
latitude: 39.739236,
longitude: -104.990251,
latitudeDelta: 0.1000,
longitudeDelta: 0.0500
},
iOS模拟器向我显示
which is relatively closer than where the iOS simulator shows me at
任何人都遇到了这个问题,并且知道如何解决它?是反应本机正在运行的网络,还是不应该从我的笔记本电脑的网络位置拉出它?
anyone run into this problem and know how to solve it? Is it the network that react-native is running on or shouldn't it be pulling from my laptops network location?
我一直在寻找是否有人遇到了这个问题问题:
React Native iOS中的地理位置
,并且如果您搜索地理位置错误的react-native ios时,堆栈搜索仅显示6条结果。Google还是没有太大帮助。
I have looked to see if anyone else ran into this issue: Geolocation in React Native iOS and the search on stack only shows 6 results if you search for geolocation wrong react-native ios...google wasn't much help either.
感谢您的帮助
推荐答案
这是iOS模拟器(和Android模拟器)上的正确行为。您可以像这样更改每个地理位置的模拟坐标(从其他答案中重新解释):
That is the correct behavior on the iOS simulator (and the Android emulator). You can change the mocked coordinates for geolocation on each like so (paraphrased from other answers):
iOS
- 运行应用在iOS模拟器中。
- 在顶部菜单栏,您会找到功能->位置->自定义位置。(或者您可以选择使用其他功能)。
- 设置纬度&位置的经度。
Android
- 在Android模拟器中运行应用程序。
- 单击工具栏中的省略号(...)。
- 在位置下新打开的设置中编辑坐标。
Android也有一个命令行替代方法使用 telnet
和 geo fix
命令在此处进行解释。
There's also a command line alternative for Android explained here using telnet
and the geo fix
command.
或者,您也可以按照ashutosh Pandey的建议在真实设备上进行测试。
Or, you can do as ashutosh pandey suggests and test on a real device.
这篇关于React Native和iOS模拟器的地理位置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!