工作這是我的javascript代碼:navigator.geolocation.getCurrentPosition/watchPosition沒有在安卓6.0
function getLocation() {
//navigator.geolocation.getCurrentPosition(getCoor, errorCoor, {maximumAge:60000, timeout:30000, enableHighAccuracy:true});
var mobile =jQuery.browser.mobile;
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if(mobile){
watchLocation(function(coords) {
var latlon = coords.latitude + ',' + coords.longitude;
//some stuff
}, function() {
alert("error");
});
} else {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("error");
}
}
}
function watchLocation(successCallback, errorCallback) {
successCallback = successCallback || function(){};
errorCallback = errorCallback || function(){};
// Try HTML5-spec geolocation.
var geolocation = navigator.geolocation;
if (geolocation) {
// We have a real geolocation service.
try {
function handleSuccess(position) {
alert("position:"+position.coords);
successCallback(position.coords);
}
geolocation.watchPosition(handleSuccess, errorCallback, {
enableHighAccuracy: true,
maximumAge: 5000 // 5 sec.
});
} catch (err) {
errorCallback();
}
} else {
errorCallback();
}
}
我曾經嘗試都getCurrentPosition
和watchPosition
。
當控制到geolocation.watchPosition
行時它達到errorCalback()
方法。
我在Motorola G 2nd Gen
與Android 6
和Google chrome browser
和opera mini
測試。
更新1:當我把錯誤的警報回撥功能我得到error:1; message:Only Secure origins are allowed(see:
link)。
navigator.geolocation.getCurrentPosition(showPosition, function(e)
{ alert(e); //alerts error:1; message:Only Secure origins are allowed(see: )
console.error(e);
})
更新2:從g4s8我能夠findout錯誤是因爲不安全的URL的幫助。即只訪問http
,而不是https
。但隨後我也繞過了瀏覽器通過點擊高級按鈕。但它會提示爲Do you want to allow location
,我不希望..有沒有任何方式訪問的位置沒有提示它?
您是否嘗試將第二個參數(錯誤回調)添加到'getCurrentPosition'方法? 'navigator.geolocation.getCurrentPosition(showPosition,function(e){console.error(e);})' – g4s8
yes ...你可以看到第二行被註釋了......'errorCoor'就是回調函數.. 。 –
你可以打印錯誤到控制檯並向此問題添加錯誤消息嗎?它可能包含有用的信息。例如'PositionError {code:1,消息:「用戶拒絕地理定位」}' – g4s8