Last active
August 29, 2015 14:02
-
-
Save skvggor/417885e7c63db14ea834 to your computer and use it in GitHub Desktop.
Temperatura [Google]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="pt-br"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hangout</title> | |
</head> | |
<body> | |
<section class="previsao-do-tempo"> | |
<section class="elementos"> | |
<section class="icone"></section> | |
<section class="temperatura"></section> | |
<section class="localidade"></section> | |
</section> | |
</section> | |
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script src="scripts.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Portal; | |
Portal = {}; | |
Portal.apps = { | |
diminuirTexto: function(texto, max) { | |
var indicador, novoTexto; | |
indicador = "…"; | |
if (texto.length >= max) { | |
novoTexto = texto.substring(0, max); | |
if (novoTexto.charAt(novoTexto.length - 1) === ' ') { | |
novoTexto = novoTexto.substring(0, max - 1); | |
} | |
texto = novoTexto + indicador; | |
} else { | |
texto = texto; | |
} | |
return texto; | |
}, | |
previsaoDoTempo: function() { | |
var converterBeauFort, _adicionarPrevisaoDoTempo; | |
converterBeauFort = function(velocidade) { | |
var tipoVento, vento; | |
tipoVento = { | |
'calmo': 'calmo', | |
'aragem': 'aragem', | |
'brisaLeve': 'brisa leve', | |
'brisaFraca': 'brisa fraca', | |
'brisaModerada': 'brisa moderada', | |
'brisaForte': 'brisa forte', | |
'ventoFresco': 'vento fresco', | |
'ventoForte': 'vento forte', | |
'ventania': 'ventania', | |
'ventaniaForte': 'ventania forte', | |
'tempestade': 'tempestade', | |
'tempestadeViolenta': 'tempestade violenta', | |
'furacao': 'furacão' | |
}; | |
if (velocidade < 0.3) { | |
vento = tipoVento.calmo; | |
} else if (velocidade === 0.3 || velocidade <= 1.5) { | |
vento = tipoVento.aragem; | |
} else if (velocidade === 1.6 || velocidade <= 3.3) { | |
vento = tipoVento.brisaLeve; | |
} else if (velocidade === 3.4 || velocidade <= 5.4) { | |
vento = tipoVento.brisaFraca; | |
} else if (velocidade === 5.5 || velocidade <= 7.9) { | |
vento = tipoVento.brisaModerada; | |
} else if (velocidade === 8.0 || velocidade <= 10.7) { | |
vento = tipoVento.brisaForte; | |
} else if (velocidade === 10.8 || velocidade <= 13.8) { | |
vento = tipoVento.ventoFresco; | |
} else if (velocidade === 13.9 || velocidade <= 17.1) { | |
vento = tipoVento.ventoForte; | |
} else if (velocidade === 17.2 || velocidade <= 20.7) { | |
vento = tipoVento.ventania; | |
} else if (velocidade === 20.8 || velocidade <= 24.4) { | |
vento = tipoVento.ventaniaForte; | |
} else if (velocidade === 24.5 || velocidade <= 28.4) { | |
vento = tipoVento.tempestade; | |
} else if (velocidade === 28.5 || velocidade <= 32.6) { | |
vento = tipoVento.tempestadeViolenta; | |
} else if (velocidade >= 32.7) { | |
vento = tipoVento.furacao; | |
} | |
return vento; | |
}; | |
_adicionarPrevisaoDoTempo = function() { | |
var container, containerLocalidade, containerTemperatura, icone; | |
container = $('.elementos'); | |
icone = $('.previsao-do-tempo .icone'); | |
containerTemperatura = $('.temperatura'); | |
containerLocalidade = $('.localidade'); | |
return navigator.geolocation.getCurrentPosition(function(posicao) { | |
var lg, lt; | |
lt = posicao.coords.latitude; | |
lg = posicao.coords.longitude; | |
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + lt + ',' + lg, function(dados) { | |
var cidade; | |
cidade = dados.results[0].address_components[3].short_name; | |
return $.getJSON('http://api.openweathermap.org/data/2.5/weather?q=' + cidade + '&units=metric&lang=pt', function(dados) { | |
var cidadeCompacto, clima, codIcone, tempMax, tempMin, vento, _exibirNomeCompactoCidade, _exibirNomeCompletoCidade; | |
cidade = dados.name; | |
cidadeCompacto = Portal.apps.diminuirTexto(cidade, 10); | |
tempMax = dados.main.temp_max.toFixed(); | |
tempMin = dados.main.temp_min.toFixed(); | |
codIcone = dados.weather[0].icon; | |
clima = dados.weather[0].description; | |
vento = dados.wind.speed; | |
if (clima === 'nuvens quebrados') { | |
clima = 'muitas nuvens'; | |
} | |
if (tempMin === tempMax) { | |
tempMin = tempMin - 1; | |
} | |
icone.css('background', 'url("http://openweathermap.org/img/w/' + codIcone + '.png") no-repeat'); | |
container.attr('title', 'Descrição: ' + clima + ' com ' + converterBeauFort(vento) + ' em ' + cidade + '.' + ' Máxima: ' + tempMax + '°, Mínima: ' + tempMin + '°'); | |
containerTemperatura.html(tempMax + '°, ' + tempMin + '° '); | |
containerLocalidade.html(cidadeCompacto); | |
_exibirNomeCompletoCidade = function(e) { | |
return containerLocalidade.html(cidade); | |
}; | |
_exibirNomeCompactoCidade = function(e) { | |
return containerLocalidade.html(cidadeCompacto); | |
}; | |
containerLocalidade.on('mouseenter', _exibirNomeCompletoCidade); | |
return containerLocalidade.on('mouseleave', _exibirNomeCompactoCidade); | |
}); | |
}); | |
}); | |
}; | |
_adicionarPrevisaoDoTempo(); | |
} | |
}; | |
Portal.apps.previsaoDoTempo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment