Here is my javascript code:
function getLocation() {
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;
}, function() {
alert("error");
});
} else {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("error");
}
}
}
function watchLocation(successCallback, errorCallback) {
successCallback = successCallback || function(){};
errorCallback = errorCallback || function(){};
var geolocation = navigator.geolocation;
if (geolocation) {
try {
function handleSuccess(position) {
alert("position:"+position.coords);
successCallback(position.coords);
}
geolocation.watchPosition(handleSuccess, errorCallback, {
enableHighAccuracy: true,
maximumAge: 5000
});
} catch (err) {
errorCallback();
}
} else {
errorCallback();
}
}
I tried both getCurrentPosition, and watchPosition.
It reaches a method errorCalback()when control comes to a string geolocation.watchPosition.
I am testing Motorola G 2nd Genwith Android 6and Google chrome browserand opera mini.
Update 1: When I put a warning in the callback function, I got a error:1; message:Only Secure origins are allowed(see: link ).
navigator.geolocation.getCurrentPosition(showPosition, function(e)
{ alert(e);
console.error(e);
})
2: g4s8 , URL. .. http https. , "". Do you want to allow location, . ?