【问题标题】:Geolocation is not working on my react native iOS emulator地理定位不适用于我的反应原生 iOS 模拟器
【发布时间】:2021-01-10 09:23:21
【问题描述】:

我在我的 react 本机代码中使用 Geolocation 来获取当前位置,它在 Android 上运行良好。但不适用于 iOS。

这是我的代码

import Geolocation from "@react-native-community/geolocation";
import Geocoder from "react-native-geocoder";

_getCurrentLocation = () => {

    Alert.alert(
        "Get Location",
        "Do you want to update your location?",
        [
            {
                text: 'Cancel',
                onPress: () => console.log('Cancel Pressed'),
                style: 'cancel'
            },
            {
                text: 'UPDATE', onPress: () => {
                    Geolocation.setRNConfiguration({ skipPermissionRequests: true,                                    authorizationLevel: "always" });
                    if (Platform.OS === 'ios') Geolocation.requestAuthorization();
                    Geolocation.getCurrentPosition(
                        info => {
                            const position = { lat: info.coords.latitude, lng: info.coords.longitude }
                            Geocoder.geocodePosition(position).then(res => {
                                console.warn(res);
                                const { updateUserState } = this.props;
                                updateUserState({ position : position, country : res[0].countryCode, city : res[0].adminArea });
                                this.setState({ position : position, country : res[0].countryCode, city : res[0].adminArea });
                            })
                                .catch(err => {
                                    console.warn(err)
                                    alert(JSON.stringify(err));
                                })
                        },
                        error => { 
                            // alert("Sorry, we cann't get your location now, please check your permission!")
                            alert(JSON.stringify(error));
                            console.log(error) 
                        },
                        {
                            enableHighAccuracy: false,
                            timeout: 3000,
                            // maximumAge: 1000,
                        },

                    )
                }
            }
        ],
        { cancelable: false }
    )


}

我收到以下错误:

PERMISSION_DENIED:1 POSITION_UNAVAILABLE:2 超时:3 代码:3 消息:“无法在 15.0 秒内获取位置。”

这是我的 info.plist:

<key>NSLocationAlwaysUsageDescription</key>
<string>This app requires access to you location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app requires access to you location.</string>

【问题讨论】:

    标签: ios react-native mobile geolocation react-native-ios


    【解决方案1】:

    请在模拟器中设置自定义latlong,Debug -> Location -> Custom 定位并设置自定义纬度和经度并再次运行应用程序

    【讨论】:

    • 我刚刚设置了自定义位置和纬度(37.785834),经度(-122.406417),然后再次运行应用程序。但结果相同。
    • 位置自定义不在调试中:功能 -> 位置 -> 自定义位置,但不是解决方案!
    猜你喜欢
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多