使用React Native和iOS模拟器的错误地理位置

问题描述:

我正在使用react-native-maps并使用react-native api进行地理定位。当我通过api使用位置时,模拟器上显示的内容显示在旧金山,而不是位于我所在的丹佛。有没有理由为什么我的位置不会显示我在哪里?使用React Native和iOS模拟器的错误地理位置

我的在运行位置

// 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 
     }, 

其在 console log of my location

比所述iPhone模拟器显示我相对更靠近任何人遇到这个问题,并知道如何解决它?这是反应 - 本地运行的网络,还是不应该从我的笔记本电脑网络位置拉?

我已经看过,看是否有人遇到了这个问题: Geolocation in React Native iOS 和堆栈中的搜索只显示6分的结果,如果你搜索地理位置错误的反应,提供原生iOS ...谷歌是没有多大帮助的。

感谢您的帮助

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

iOS

  1. 运行的应用程序。
  2. 在顶部菜单栏中,您会看到调试 - >位置 - >自定义位置..(或者您可以选择使用其他)。
  3. 设置位置的纬度&经度。

Android

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

还有使用telnetgeo fix命令为Android explained here命令行替代。

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

+0

有趣的是,谢谢你让我知道如何解决这个问题,所以我可以测试我的地理位置! –

地理位置不上仿真工作既Android和iOS。 如果你会检查它真正的设备上,它会正常工作。