我正在使用openweathermap制作天气应用程序。它在桌面上工作正常,但在移动设备上,地理定位在某种程度上无法正常工作,无法执行 成功或错误功能。
我在使用https://app.crossbrowsertesting.com的许多设备上试过这个 但是每个移动设备在超时后都会抛出一个错误(桌面工作正常):
$('document').ready( function(){
$('.container-fulid').hide();
if(!navigator.geolocation)
{
// throw error that browser does not support geolocations
$('.loader').hide();
$('body').html('<h2>GeoLocations are not Supported by your browser Please try again with another Browser</h2>');
// .container-fulid is still hidden
}
function sucess(position){
var longi =position.coords.longitude;
var lati = position.coords.latitude;
console.log(longi + " "+ lati);
$.ajax({
url : "https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?lat=" + lati + "&lon=" + longi + "&units=metric&APPID=dd29378e1f324bc034657edf0927cdf5",
success : function (data){
console.log(data);
$("#city").text( data.name + "," + data.sys.country);
$("#condition").attr("src" ,"http://openweathermap.org/img/w/" + data.weather[0].icon +".png");
$("#discription").text(data.weather[0].description);
$("#temp").text(Math.floor(data.main.temp) +" " +'\xB0' );
$("#max").text(data.main.temp_max);
$("#min").text(data.main.temp_min);
$("#right p:nth-child(1)").text("Humidity - " + data.main.humidity + " %");
$("#right p:nth-child(2)").text("Pressue - " + data.main.pressure + " Pa");
$("#right p:nth-child(3)").text("Wind - " + data.wind.speed + " Meter/Sec");
//set background image
var d = new Date();
imagePicker(data.main.temp , d.getHours() );
},
error : function (xhr,status,error){
$('body').html('<h2>' + xhr.status+' !<br>' +xhr.statusText +'<br> </h2>' + xhr.statusCode );
console.log(xhr);
}
});
}
function error(err)
{
$('body').html('<h2>' + err.message+' !<br> Please try again !!</h2>' + ' <h3>Error Code ' +err.code+'</h3>' );
console.log(err);
}
navigator.geolocation.getCurrentPosition(sucess, error, {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
});
这是我在移动设备上遇到的错误:
Timeout expired error code : 3
以下是我的代码的链接:
https://codepen.io/Priyansh1161/pen/pyBwoE?editors=0010