我如何使用地理定位替代教程的天气



我找到了这个教程-http://tutorialzine.com/2013/05/weather-web-app-geolocation-revisited/

但它使用地理定位。有人知道如何改变这个吗?这样我就可以插入一个位置或使用喜欢的东西http://freegeoip.net找到一个位置?我一直在尝试,但我最终得到了一个错误代码,或者它永远不会加载。

这看起来很直接。

删除:

// Does this browser support geolocation?
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
}
else{
    showError("Your browser does not support Geolocation!");
}

用你自己的坐标称之为:

function locationSuccess(position)

位置参数可以有你想要的任何结构,只需确保更新这些行以匹配你的变量。

var weatherAPI = 'http://api.openweathermap.org/data/2.5/forecast?lat='+position.coords.latitude+
                                '&lon='+position.coords.longitude+'&callback=?'

您希望手动调用locationSuccess,而不是检查地理位置是否可用的代码。

最新更新