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

Sap*_*hra 5 javascript google-maps google-maps-api-3 ios

您好,我正在使用下面的 java 脚本获取位置权限,当我们在桌面浏览器和 Android 设备上打开它时工作正常,但弹出窗口在 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();
        })();
Run Code Online (Sandbox Code Playgroud)

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

kat*_*nco 0

大家好[我正在使用 Angularjs 开发 Apache Cordova 应用程序],我终于想出了一个用户在 Safari 中不允许位置权限的解决方案。然后我只是向用户发出一个对话框提醒,通过该对话框他或她可以如何在 Safari 中启用位置服务。如下所示[ and 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 ");
                            }
    
                        }
                    ); 
Run Code Online (Sandbox Code Playgroud)