所以我现在正在尝试制作一个基于用户位置显示天气信息的小应用程序。我可以轻松地将值传递给我的纬度和经度以使其正常工作,但我似乎无法获取坐标并将它们分配给我的变量。我错过了什么?
注意:我是 javascript 的新手,我敢打赌这是愚蠢的。
var latitude = 0;
var longitude = 0;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocation);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function displayLocation(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
return;
}
jQuery(document).ready(function($) {
$.ajax({
url : "http://api.wunderground.com/api/7c5cbc3e4db2885a/geolookup/conditions/q/" + latitude + "," + longitude + ".json",
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
var desc = parsed_json['current_observation']['weather'];
var wind_dir = parsed_json['current_observation']['wind_dir'];
("Current temperature in " + location + " is: " + temp_f);
$('#location').html(location);
$('#temp_f').html(temp_f);
$('#desc').html(desc);
$('#wind_dir').html(wind_dir);
$('#icon_url').attr('http://icons.wxug.com/i/c/i/clear.gif', img);
}
});
});