在iOS移动浏览器中请求位置权限弹出窗口

5

你好,我正在使用下面的JavaScript代码请求位置权限。在桌面浏览器和安卓设备上运行良好,但在iOS设备上弹出窗口无法正常工作。

(function () {
            function updatePermission(name, state) {
                console.log('update permission for ' + name + ' with ' + state);
            }

            function init() {
                var getPosBtn = document.querySelector('#getPositionBtn');
                // Check for Geolocation API permissions
                navigator.permissions.query({name:'geolocation'}).then(function(p) {
                    updatePermission('geolocation', p.state);
                    p.onchange = function() {
                        updatePermission('geolocation', this.state);
                    };
                });

                getPosBtn.addEventListener('click', function() {
                    getLocation();
                });

                $( document ).ready(function() {
                    getpermission();
                });

                function getpermission(){
                    navigator.geolocation.getCurrentPosition(
                        function(position) {
                            var geocoder;
                            var city;
                            geocoder = new google.maps.Geocoder();
                            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                            geocoder.geocode({'latLng': latlng},function (results, status) {
                                if (status == google.maps.GeocoderStatus.OK) {
                                    if (results[0]) {
                                        var arrAddress = results;
                                        console.log(results);
                                    } else {
                                        console.log("address not found");
                                    }
                                } else {
                                    console.log("Geocoder failed due to: " + status);
                                }
                            });
                        },function(error) {
                            console.log(error);
                        },{enableHighAccuracy: true, maximumAge: 10000} 
                    );
                }

                function getLocation(){
                    navigator.geolocation.getCurrentPosition(
                        function(position) {
                            var geocoder;
                            var city;
                            geocoder = new google.maps.Geocoder();
                            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                            geocoder.geocode({'latLng': latlng},function (results, status) {
                                if (status == google.maps.GeocoderStatus.OK) {
                                    if (results[0]) {
                                        var arrAddress = results;
                                        console.log(results);
                                        $('.location_input').val(results[0].formatted_address);
                                    } else {
                                        console.log("address not found");
                                    }
                                } else {
                                    console.log("Geocoder failed due to: " + status);
                                }
                            });
                        },function(error) {
                            console.log(error);
                        },{enableHighAccuracy: true, maximumAge: 10000} 
                    );
                }

            }
            init();
        })();

当用户点击位置图标和页面加载时,我会在两种情况下向用户请求权限。页面加载的权限在任何设备上都不起作用,但是 Android 设备上按钮事件的点击权限是有效的。


1
有找到任何解决方案吗?请帮帮我,我也遇到了同样的问题。 - kodali
1个回答

0
大家好,我正在使用Angularjs开发Apache Cordova应用程序。最近我遇到了一个问题,就是用户在Safari中没有允许位置权限。然后我通过弹出对话框告诉用户如何在Safari中允许位置服务。具体如下:当Error.Code==1时表示用户拒绝了位置权限。在获得权限后,错误就消失了 :)
 $scope.watchId = navigator.geolocation.watchPosition(function (position) {
                        if ($scope.t1 == 0) {
                            $scope.t1 = position.timestamp;
                        } else {
                            if (Math.abs($scope.t1 - position.timestamp) > 5000) {
                                $scope.t1 = position.timestamp;
                                SellerRef.update({ Location: { Lat: position.coords.latitude, Long: position.coords.longitude } })
                            }
                        }
    
                    },
                        function (error) {
                            if (error.code == 1) {
                                  $scope.showAlert("An error occured \n" + " Please goto Settings->Privacy->LocationServices and give permission for " + bowser.name + " which is your browser ");
                            }
    
                        }
                    ); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接