woeid.js 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. $("#weather_query").click(function(){
  2. query_woeid();
  3. });
  4. $("input:text[name=weather_place]").keydown(function(event){
  5. if (event.which == 13)
  6. {
  7. event.preventDefault();
  8. }
  9. })
  10. function query_woeid()
  11. {
  12. weather_place = $("input:text[name=weather_place]").val();
  13. url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22" + encodeURI(weather_place) + "%22&format=json" ;
  14. $.getJSON( url, {
  15. tags: "woeid",
  16. tagmode: "any",
  17. format: "json"
  18. })
  19. .done(function( data ) {
  20. if (data["query"]["results"] != null)
  21. {
  22. alert("Informations récupérés!");
  23. var place = data["query"]["results"]["place"];
  24. woeid = place["woeid"];
  25. town = place["name"];
  26. country = place["country"]["content"];
  27. region = place["admin1"]["content"];
  28. $("input[name=weather_place]").val(town + " " + region + " " + country);
  29. $("input[name=woeid]").val(woeid);
  30. }
  31. else
  32. {
  33. alert("Aucune informations trouvée! :-(")
  34. }
  35. })
  36. .fail(function(){
  37. alert("Impossible de récupérer l'informations!");
  38. });
  39. }