navigator.geolocation.getCurrentPosition not triggering in iOS simulator with React Native

佐手、 提交于 2019-12-11 08:58:41

问题


In Android emulators, the below code works fine but in iOS, it doesn't even step into navigator.geolocation.getCurrentPosition.

I've updated my simulator's custom location through the debug menu but if it's not even getting into navigator.geolocation.getCurrentPosition I'm a bit lost.

constructor(props) {
    super(props);
    this.state = {
        focusedLocation: {
            latitude: 37.4219999,
            longitude: -122.0862462,
            latitudeDelta: 0.0022,
            longitudeDelta: Dimensions.get("window").width / Dimensions.get("window").height * 0.0122,
        },
    };
}    

componentDidMount() {

    console.log("did mount: ", this.state.grumblersLocation.latitude); //triggers iOS and Android

    navigator.geolocation.getCurrentPosition(pos => {
            console.log("raw pos lat: ", pos.coords.latitude); //triggers in Android only
            this.setState({
                focusedLocation: {
                    ...this.state.grumblersLocation,
                    latitude: pos.coords.latitude,
                    longitude: pos.coords.longitude
                }
            }, () => {
                console.log("state after pos lat: ", this.state.grumblersLocation.latitude);  //triggers in Android only
            });

        },
        err => {
            console.log("Fetching the Position failed: ", err); //Not triggering on either
        });

}

回答1:


EDIT:The pop up never appeared in IOS for me because I was adding the Privacy message info to the wrong info.plist file. It needs to go here:

EDIT: One other fix that worked for me in iOS was:

  1. Hit he home button on the simulator
  2. Click the Settings app>Privacy>Location Services>My App
  3. Then make sure the "While Using the App" was checked



来源:https://stackoverflow.com/questions/50424843/navigator-geolocation-getcurrentposition-not-triggering-in-ios-simulator-with-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!