我想获取用户的当前位置,我在 android 和 ios 中获取当前位置,但在 android 平板电脑上获取不到当前位置

发布于 2025-01-15 17:07:08 字数 2447 浏览 0 评论 0原文

我在 android 和 ios 中获得了正确的纬度和经度并显示了标记,但这在平板电脑上不会发生。我没有在平板电脑 android 中获得标记,我也启用了 showUserLocation={true} 。

useEffect(() => {
    const requestLocationPermission = async () => {
        if (Platform.OS === 'ios') {
            Geolocation.requestAuthorization('always').then((resp) => {
                console.log("ios response ", resp)
                Geolocation.getCurrentPosition(
                    (position) => {
                        console.log("position:::", position)
                        const { latitude, longitude } = position.coords;
                        setCurrentLatitude(latitude)
                        setCurrentLongitude(longitude)
                    },
                    (error) => {
                        // See error code charts below.
                        console.log(error.code, error.message);
                    },
                    { enableHighAccuracy: true, timeout: 20000 }
                );
            }).catch((e) => console.log("ios error ::", e))
        }
        else {
            try {
                const granted = await PermissionsAndroid.request(
                    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
                    {
                        title: 'Location Access Required',
                        message: 'This App needs to Access your location',
                    },
                );
                if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                    Geolocation.getCurrentPosition(
                        (position) => {
                            // console.log("position:::", position)
                            const { latitude, longitude } = position.coords;
                            setCurrentLatitude(latitude)
                            setCurrentLongitude(longitude)
                        },
                        (error) => {
                            // See error code charts below.
                            console.log(error.code, error.message);
                        },
                        { enableHighAccuracy: false, timeout: 20000 }
                    );

                } else {
                    setLocationStatus('Permission Denied');
                }
            } catch (err) {
                console.warn("Location error ::", err);
            }
        };
    }
    requestLocationPermission();
}, [])

这就是我所做的

I am getting correct latitude and longitude in android and ios and showing the marker but this doesn't happen on tablet. I am not getting marker in tablet android , I have enable showUserLocation={true} as well.

useEffect(() => {
    const requestLocationPermission = async () => {
        if (Platform.OS === 'ios') {
            Geolocation.requestAuthorization('always').then((resp) => {
                console.log("ios response ", resp)
                Geolocation.getCurrentPosition(
                    (position) => {
                        console.log("position:::", position)
                        const { latitude, longitude } = position.coords;
                        setCurrentLatitude(latitude)
                        setCurrentLongitude(longitude)
                    },
                    (error) => {
                        // See error code charts below.
                        console.log(error.code, error.message);
                    },
                    { enableHighAccuracy: true, timeout: 20000 }
                );
            }).catch((e) => console.log("ios error ::", e))
        }
        else {
            try {
                const granted = await PermissionsAndroid.request(
                    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
                    {
                        title: 'Location Access Required',
                        message: 'This App needs to Access your location',
                    },
                );
                if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                    Geolocation.getCurrentPosition(
                        (position) => {
                            // console.log("position:::", position)
                            const { latitude, longitude } = position.coords;
                            setCurrentLatitude(latitude)
                            setCurrentLongitude(longitude)
                        },
                        (error) => {
                            // See error code charts below.
                            console.log(error.code, error.message);
                        },
                        { enableHighAccuracy: false, timeout: 20000 }
                    );

                } else {
                    setLocationStatus('Permission Denied');
                }
            } catch (err) {
                console.warn("Location error ::", err);
            }
        };
    }
    requestLocationPermission();
}, [])

This is what I have done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文