我试图使用geolocations获取当前坐标,然后将输入到打开天气api以获取数据并通过html显示它。脚本没有响应打开天气api
这是我的脚本:
var lat =0;
var long =0;
function getLocation(){
var x = document.getElementByID('demo');
if (navigator.gelocation){
navigator.gelocation.getCurrentPosition(function(position){
lat = position.coords.latitude;
long = position.coord.longitude;
$('#coord').html('Here:'+lat+ 'here'+long);
});
} else {
x.innerHTML= 'Geolocation not supported';
}
};
function getWeather(callback){
$.ajax({
dataType:"jsonp",
url:'http://api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+long+'&APPID=f2d2480cdd1ef46b9f10dd6860cb1a3e',
succes: callback
});
}
getWeather(function(data){
var temp = data.main.temp;
var description = data.weather[0].description;
$("#temp").html(data.main.temp);
$('#desc').html(data.weather[0].description);
});
$('document').ready(function(){
getLocation();
getWeather();
});
,这是HTML:
<div id = "city">
<h4>city</h4>
</div>
<div id="temp">
<h4>temp</h4>
</div>
<div id= "desc">
<h4>description</h4>
</div>
<div id= "coord">
<h4>coord</h4>
</div>
我目前做的codepen.io这个项目,这里的link我真的不确定我是什么做错了。我也查看了其他示例代码,我无法发现我正在犯的错误。我能否请帮助一下,为什么代码似乎没有回应。
看来你没有任何元素与“演示”的ID。一探究竟。我认为你应该删除$('document')中的单引号。此外正确的是:getElementByID - > getElementById –
我解决了一些问题,但你是否也想调用coords?我没有看到他们在jQuery代码中的任何地方。 – ZombieChowder