已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用 在线演示 本地下载 今天我们介绍来自tutorialzine的一个HTML5/jQuery/Yahoo API的开发教程,在这篇文章中我们将介绍如何使用HTML5的Geolocation,jQuery和YahooAPI来开发一个天气预报web应用。 如果你不熟悉HTML5的Geolocation(地理位置服务),请参考我们的HTML5教程: HTML5 Geolocation。首先你需要得到Yahoo API的API key,你可以通过如下地址取得对应的API key:/dashboard/createKey.html以上创建过程中会要求你输入相关应用地址等信息。创建成功后,你可以得到APPID。主要思路在这个教程中,我们主要思路如下:1. 使用Geolocation取得用户的地理位置信息 2. 然后,使用yahoo的 PlaceFinder API,来通过经纬度来找到具体地点,例如,城市或者国家。 其中包括了woeid,这个用来在天气预报应用中找到国家 3. 最后,我们将调用yahoo的Weather API来取得天气 web应用代码HTML Weather Forecast with jQuery & Yahoo APIs Weather Forecast Previous Next Tutorial: Weather Forecast with jQuery & Yahoo APIs Head on to Tutorialzine to download this example Javascript$(function() /* Configuration */ var APPID = fa2pT26k; / Your Yahoo APP id var DEG = c; / c for celsius, f for fahrenheit / Mapping the weather codes returned by Yahoos API / to the correct icons in the img/icons folder var weatherIconMap = storm, storm, storm, lightning, lightning, snow, hail, hail, drizzle, drizzle, rain, rain, rain, snow, snow, snow, snow, hail, hail, fog, fog, fog, fog, wind, wind, snowflake, cloud, cloud_moon, cloud_sun, cloud_moon, cloud_sun, moon, sun, moon, sun, hail, sun, lightning, lightning, lightning, rain, snowflake, snowflake, snowflake, cloud, rain, snow, lightning ; var weatherDiv = $(#weather), scroller = $(#scroller), location = $(p.location); / Does this browser support geolocation? if (navigator.geolocation) navigator.geolocation.getCurrentPosition(locationSuccess, locationError); else showError(Your browser does not support Geolocation!); / Get users location, and use Yahoos PlaceFinder API / to get the location name, woeid and weather forecast function locationSuccess(position) var lat = position.coords.latitude; var lon = position.coords.longitude; / Yahoos PlaceFinder API /geo/placefinder/ / We are passing the R gflag for reverse geocoding (coordinates to place name) var geoAPI = /geocode?location=+lat+,+lon+&flags=J&gflags=R&appid=+APPID; / Forming the query for Yahoos weather forecasting API with YQL / /weather/ var wsql = select * from weather.forecast where woeid=WID and u=+DEG+, weatherYQL = /v1/public/yql?q=+encodeURIComponent(wsql)+&format=json&callback=?, code, city, results, woeid; if (window.console & ) (Coordinates: %f %f, lat, lon); / Issue a cross-domain AJAX request (CORS) to the GEO service. / Not supported in Opera and IE. $.getJSON(geoAPI, function(r) if(r.ResultSet.Found = 1) results = r.ResultSet.Results; city = results0.city; code = results0.statecode | results0.countrycode; / This is the city identifier for the weather API woeid = results0.woeid; / Make a weather API request: $.getJSON(weatherYQL.replace(WID,woeid), function(r) if(r.query & r.query.count = 1) / Create the weather items in the #scroller UL var item = r.query.results.channel.item.condition; if(!item) showError(We cant find weather information about your city!); if (window.console & ) (%s, %s; woeid: %d, city, code, woeid); return false; addWeather(item.code, Now, item.text + +item.temp+DEG+); for (var i=0;i2;i+) item = r.query.results.channel.item.forecasti; addWeather( item.code, item.day + +item.date.replace(d+$,)+, item.text + +item.low+DEG+ / +item.high+DEG+ ); / Add the location to the page location.html(city+, +code+); weatherDiv.addClass(loaded); / Set the slider to the first slide showSlide(0); else showError(Error retrieving weather data!); ); ).error(function() showError(Your browser does not support CORS requests!); ); function addWeather(code, day, condition) var markup = + + + day + + condition + ; scroller.append(markup); /* Handling the previous / next arrows */ var currentSlide = 0; weatherDiv.find(a.previous).click(function(e) e.preventDefault(); showSlide(currentSlide-1); ); weatherDiv.find(a.next).click(function(e) e.preventDefault(); showSlide(currentSlide+1); ); function showSlide(i) var items = scroller.find(li); if (i = items.length | i 0 | scroller.is(:animated) return false; weatherDiv.removeClass(first last); if(i = 0) weatherDiv.addClass(first); else if (i = items.length-1) weatherDiv.addClass(last); scroller.animate(left:(-i*100)+%, function() currentSlide = i; ); /* Error handling functions */ function locationError(error) switch(error.code) case error.TIMEOUT: showError(A timeout occured! Please try again!); break; case error.POSITION_UNAVAILABLE: showError(We cant detect your location. Sorry!); break; case error.PERMISS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年金堂检察招聘备考题库及答案详解(易错题)
- 河北吴桥杂技艺术学校2026年度高层次人才选聘的备考题库参考答案详解
- 2026年上海市杨浦区复旦大学经济学院招聘经济学院专业硕士研究生教育行政管理岗位的备考题库及答案详解参考
- 成都大学附属医院2025年公开考核招聘高层次人才备考题库及答案详解参考
- 2026年重庆人才服务股份有限公司派遣至重庆轮船(集团)有限公司交运游轮分公司招聘备考题库附答案详解
- 2026年校园招聘深圳市罗外教育集团校园招聘备考题库及参考答案详解一套
- 2026年中国雄安集团有限公司校园招聘备考题库及答案详解(新)
- 2026年德州一地事业单位招聘备考题库及完整答案详解1套
- 江苏省沿海开发集团有限公司所属企业2025年度长期招聘备考题库及一套参考答案详解
- 2026年重庆机床(集团)有限责任公司磐联传动科技分公司招聘6人备考题库及一套参考答案详解
- DBJ33T 1290-2023 装配式部分包覆钢-混凝土组合结构技术规程
- 毛石混凝土设计规范
- 风机盘管维修培训
- 2024年天津高考英语第二次高考真题(原卷版)
- 2024年-Juniper防火墙配置
- 线虫病疫木及异常枯死松树处置 投标方案案(技术方案)
- 季度安全工作汇报
- (高清版)DZT 0350-2020 矿产资源规划图示图例
- 广西中医药大学赛恩斯新医药学院体育补考申请表
- 公司委托法人收款到个人账户范本
- 2023年上海市春考数学试卷(含答案)
评论
0/150
提交评论