除了當前位置顯示在其他位置這一事實之外,一切都適用於我的代碼。我住在Kuils River,它給了我Edgemead的天氣條件。如何獲得我所在的當前位置?任何人都可以幫我解決這個問題。如何根據用戶當前的GPS位置獲取天氣狀況
$(document).ready(function() {
var lat, lon, api_url;
if ("geolocation" in navigator) {
$('#showTemp').on('click', function() {
navigator.geolocation.getCurrentPosition(gotLocation);
function gotLocation(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
api_url = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&units=metric&appid=b231606340553d9174136f7f083904b3';
APPID: '53f9d8e4213222cf517d86dc406d67fc'
$.ajax({
url: api_url,
method: 'GET',
success: function(data) {
var tempr = data.main.temp;
var location = data.name;
var desc = data.weather.description;
$('#result').text(tempr + '°' + location);
}
});
}
});
} else {
alert('Your browser doesnt support geolocation. Sorry.');
}
});
這不是代碼嗎? – epascarello
這與你的代碼沒有關係,因爲它是正確的。地理位置可能不精確 – Roman
代碼給我的天氣和位置,但地區是錯誤的我住在Kuils河它給了我一個位置名稱Edgemead – Grampiano