【发布时间】: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