我試圖使用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