无法使用JavaScript获取位置
我正在尝试创建一个天气应用程序,它使用JSON API获取您所在位置的天气。为此,我需要用户的位置无法使用JavaScript获取位置,javascript,jquery,weather,weather-api,Javascript,Jquery,Weather,Weather Api,我正在尝试创建一个天气应用程序,它使用JSON API获取您所在位置的天气。为此,我需要用户的位置 $(document).ready(function(){ // gets user's location; shows Earth weather if location cannot be accessed var longitude = 0; var latitude = 0; if (navigator.geolocation) {
$(document).ready(function(){
// gets user's location; shows Earth weather if location cannot be accessed
var longitude = 0;
var latitude = 0;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
latitude = Math.floor(position.coords.latitude);
longitude = Math.floor(position.coords.longitude);
});
}
$.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=44db6a862fba0b067b1930da0d769e98", function(json){
// gets data from json
if (json.name == "Earth") {
$("#city").html("Your browser is not giving me access to your location. \n Showing Earth weather instead.");
} else {
$("#city").html(json.name);
$("#country").html(", " + json.sys.country);
}
var weather = json.weather[0].main;
$("#weather").html(weather);
var tempInKelvin = parseFloat(json.main.temp);
var tempInCelsius = Math.round((tempInKelvin - 273.15)*10)/10;
var tempInFahrenheit = tempInCelsius + 32;
$("#temperature").html(tempInFahrenheit); // shows temperature in Fahrenheit by default
// switches between Fahrenheit and Celsius when clicked
var iterator = 1; // because .toggle() was deprecated in jQuery 1.9
$("#unit").on("click", function(){
if (iterator % 2 == 1) {
$("#unit").html("℃"); // change to Celsius
$("#temperature").html(tempInCelsius);
} else {
$("#unit").html("℉"); // change back to Fahrenheit
$("#temperature").html(tempInFahrenheit);
}
iterator++;
});
// Changes background according to time of day
var time = new Date();
time = time.getHours();
// adds icon, depending on time and weather
switch (weather.toLowerCase()) {
case "clouds":
$("#icon").html('<p style = "color: white;">☁</p>');
break;
case "rain":
$("#icon").html('<p style = "color: blue;">☂</p>');
break;
case "snow":
$("#icon").html('<p style = "color: blue;">❄</p>');
break;
case "clear":
if (time >= 19 || time <= 4) {
$("#icon").html("fa-moon-o");
} else {
$("#icon").addClass("fa-sun-o");
}
break;
default:
$("#icon").html("No icon found :(");
}
});
});
$(文档).ready(函数(){
//获取用户的位置;如果无法访问该位置,则显示地球天气
var经度=0;
var纬度=0;
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
纬度=数学楼层(位置坐标纬度);
经度=数学地板(位置坐标经度);
});
}
$.getJSON(“http://api.openweathermap.org/data/2.5/weather?lat=“+纬度+”&lon=“+经度+”&appid=44db6a862fba0b067b1930da0d769e98”,函数(json){
//从json获取数据
如果(json.name==“地球”){
$(“#城市”).html(“您的浏览器不允许我访问您的位置。\n而是显示地球天气。”);
}否则{
$(“#city”).html(json.name);
$(“#country”).html(“,”+json.sys.country);
}
var weather=json.weather[0].main;
$(“#weather”).html(天气);
var tempInKelvin=parseFloat(json.main.temp);
var tempInCelsius=数学圆((tempInKelvin-273.15)*10)/10;
var tempInFahrenheit=tempInCelsius+32;
$(“#temperature”).html(tempInFahrenheit);//默认情况下以华氏温度显示温度
//单击时在华氏温度和摄氏温度之间切换
var iterator=1;//因为.toggle()在jQuery 1.9中被弃用
$(“#单位”)。在(“单击”,函数(){
如果(迭代器%2==1){
$(“#单位”).html(℃”);//更改为摄氏度
$(“#温度”).html(tempInCelsius);
}否则{
$(“#单位”).html(℉”)//更改回华氏温度
$(“#温度”).html(tempinghernheit);
}
迭代器++;
});
//根据一天中的时间更改背景
变量时间=新日期();
time=time.getHours();
//根据时间和天气添加图标
开关(weather.toLowerCase()){
案例“云”:
$(“#icon”).html(“☁;
”);
打破
“雨”一案:
$(“#icon”).html(“☂;
”);
打破
案例“雪”:
$(“#icon”).html(“❄
”);
打破
案例“明确”:
如果(time>=19 | | time,则问题在于在得到回调的结果之前执行代码
navigator.geolocation.getCurrentPosition()
您需要执行代码async,等待获得成功回调的结果,该结果将更新您的坐标值。可能重复的@Andreas部分正确,这是关于异步调用的,请参阅下面的答案,以了解这一点。试试这个,我怎么做?我对Ja完全陌生vaScript@HumanCyborgRelations,尝试上面为您提供的代码片段Faisal,这是一个很好的实现