react-native - navigator.geolocation.getCurrentPosition 在使用 React Native 的 iOS 模拟器中未触发

标签 react-native geolocation ios-simulator react-native-maps

在 Android 模拟器中,下面的代码工作正常,但在 iOS 中,它甚至不进入 navigator.geolocation.getCurrentPosition。

我已经通过调试菜单更新了模拟器的自定义位置,但如果它甚至没有进入 navigator.geolocation.getCurrentPosition,我就有点迷失了。

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
        });

}

最佳答案

编辑:该弹出窗口从未出现在 IOS 中,因为我将隐私消息信息添加到了错误的 info.plist 文件中。它需要去这里: enter image description here

编辑:在 iOS 中对我有用的另一个修复是:

  1. 点击模拟器上的主页按钮
  2. 点击“设置” 应用>隐私>定位服务>我的应用
  3. 然后确保“使用时 该应用程序”已被选中

enter image description here

关于react-native - navigator.geolocation.getCurrentPosition 在使用 React Native 的 iOS 模拟器中未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50424843/

相关文章:

ios - Flutter 地理定位器插件 Geolocator().getCurrentPosition 不返回准确的 lng 和 lat 坐标

ios - 如何在 iOS 模拟器中测试动态类型(更大的字体大小)

ios - iPhone 5 iOS 6 模拟器 imagePicker DidFinish 不工作

react-native - 如何在 React Native 中进行签名捕获?

macos - 我可以在 docker 镜像中开发 react-native 应用吗

ios - 如何在不打开应用程序的情况下使用React-Native在Instagram上发布内容?

IOS模拟器9.0不运行

android - 如何以编程方式设置 3D 人形模型的姿势?

android - 如何使用 Here Map for android SDK 获取经纬度?

javascript - 从 jQuery GET JSON 响应字符串获取单个值