【问题标题】:Ask for location permission popup in iOS mobile browser在 iOS 移动浏览器中请求位置权限弹出窗口
【发布时间】:2018-10-24 20:23:26
【问题描述】:

您好,我正在使用下面的 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();
        })();

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

【问题讨论】:

  • 找到任何解决方案了吗?请帮助我,我也遇到了同样的问题。

标签: javascript ios google-maps google-maps-api-3


【解决方案1】:

大家好[我正在使用 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 ");
                            }
    
                        }
                    ); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    相关资源
    最近更新 更多