目前正在爲iOS的天氣web應用程序工作。我試圖獲取用戶的座標,並將緯度和經度放在我的Ajax請求中(在api Wunderground上)。獲取ajax請求的位置座標
這裏是我的代碼(編輯):
<script type="text/javascript">
$(document).ready(function() {
navigator.geolocation.getCurrentPosition(getLocation, unknownLocation);
function getLocation(pos)
{
var lat = pos.coords.latitude;
var lon = pos.coords.longitude;
$.ajax({
url : "http://api.wunderground.com/api/ApiId/geolookup/conditions/q/"+ lat +","+ lon +".json",
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
alert("Current temperature in " + location + " is: " + temp_f);
}
});
}
function unknownLocation()
{
alert('Could not find location');
}
});
</script>
正如你可以看到我「簡單的」要創建2瓦爾緯度和經度,在我的要求添加。我嘗試了很多變體,但我無法獲得此代碼的工作。
任何想法我做錯了什麼?
非常感謝!
您是否註冊了API密鑰?看起來你需要一個。 – epascarello
我有一個,我只是隱藏在我的代碼,因爲你不需要它:) – MonBlaze