【发布时间】:2017-12-27 21:30:56
【问题描述】:
除了当前位置显示在其他地方之外,一切都适用于我的代码。我住在 Kuils River,它为我提供了 Edgemead 的天气状况。我如何获得我所在的当前位置?谁能帮我解决这个问题。
$(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.');
}
});
【问题讨论】:
-
那段代码不是这样做的吗?
-
这与您的代码无关,因为它是正确的。地理位置可能不精确
-
代码给了我天气和位置,但区域错误我住在 Kuils River 它给了我一个位置名称 Edgemead
-
I live in Kuils River and it gives me the weather conditions for Edgemead这只是由于 API 或地理位置不准确造成的。代码本身很好。您唯一的解决方案是使用具有更好 GPS 模块的设备,或者找到在区域报告方面具有更高粒度的 API
标签: javascript jquery geolocation location weather-api