1

我正在使用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 ...谷歌是沒有多大幫助的。

感謝您的幫助

回答

2

這是iOS模擬器(和Android模擬器)上的正確行爲。您可以更改地理位置的座標嘲笑每個像這樣(從其他答案轉述):在iOS的模擬器

iOS

  1. 運行的應用程序。
  2. 在頂部菜單欄中,您會看到調試 - >位置 - >自定義位置..(或者您可以選擇使用其他)。
  3. 設置位置的緯度&經度。

Android

  1. 在Android模擬器運行應用程序。
  2. 單擊工具欄上的省略號(...)。
  3. 在位置下的新打開的設置中編輯座標。

還有使用telnetgeo fix命令爲Android explained here命令行替代。

或者,您可以按照ashutosh pandey建議並在真實設備上測試。

+0

有趣的是,謝謝你讓我知道如何解決這個問題,所以我可以測試我的地理位置! –

-1

地理位置不上仿真工作既Android和iOS。 如果你會檢查它真正的設備上,它會正常工作。

相關問題