在iOS移动浏览器

时间:2018-05-15 05:18:07

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

您好我在下面使用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设备。

1 个答案:

答案 0 :(得分:0)

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