Я знаю, что «но это работает на моем компьютере» - это одна из вещей, которую я никогда не должен говорить, но мы здесь.
У меня есть небольшое приложение, которое узнает ваше местоположение, вашу погоду и даст вам музыкальный плейлист.
Используя файлы локально, вы можете использовать геолокацию ИЛИ искать в любом месте по имени.
В моей развернутой версии S3 поиск любого местоположения просто выводит ваши результаты геолокации. Поэтому форма ввода и поиска не работает.
Вот код:
Местоположение ввода:
$("#input-location").click(function(event){
event.preventDefault();
$('#mix-display').show();
$('#skipButton').show();
getWeatherWithUserInput()
.then(function(response) {
weatherCode = response;
showMix(weatherCode);
})
});
Получить местоположение с геолокации:
$('#get-location').click(function(event){
event.preventDefault();
$('#mix-display').show();
$('#skipButton').show();
getWeatherWithGeo()
.then(function(response) {
weatherCode = response;
showMix(weatherCode);
})
});
Вот две погодные функции (я понимаю, что здесь происходит много глупого повторения кода, в конце концов я исправлю это!):
function getWeatherWithGeo() {
return new Promise(function(resolve,reject) {
getGeoLocationGoogle()
.then(function(response) {
var lat = response.location.lat;
var lon = response.location.lng;
var weatherLLQueryURL = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=" + WeatherAPIKey;
$.ajax({
url: weatherLLQueryURL,
method: "GET"
}).done(function(response) {
$(".wind").html("<h2>" + response.name + ": </h2>");
var f_temp = 9/5*(response.main.temp-273)+32;
$(".temp").html("<h2>" + Math.floor(f_temp) + "℉</h2>");
resolve(response.weather[0].id);
});
})
})
}
Получить погоду от ввода:
function getWeatherWithUserInput() {
return new Promise(function(resolve, reject) {
var location = $("#location").val().trim();
var weatherCSQueryURL = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + WeatherAPIKey;
$.ajax({
url: weatherCSQueryURL,
method: "GET"
}).done(function(response) {
$(".wind").html("<h2>" + response.name + ": </h2>");
var f_temp = 9/5*(response.main.temp-273)+32;
$(".temp").html("<h2>" + Math.floor(f_temp) + "℉</h2>");
resolve(response.weather[0].id);
});
});
};
полный код здесь: https://github.com/nicktamburro/YouAreListeningToWeather
развернутая версия здесь: http://www.youarelisteningtoweather.com