关于实现手机端自动获取天气的demo

首先分析需求,移动端实现天气查询。如果在没有给定地理位置的时候,那很简单,直接利用JSONP获取Open Weather API提供的一个接口,传个位置就好。这点我就不多说了,下面我要说的是,实现自动定位并获取天气。


首先要做准备工作:百度Map开发者的accesskey,OpenWeather开发者的accesskey。这个注册一下就可以免费试用,还是蛮良心的,哈哈。


现在开始第一步,首先我们要获取经纬度,这里博主用的是H5提供的一个api接口navigator.geolocation,当然也可以利用访问IP获取经纬度或城市,但是博主搜了很多,大部分不对外开放了


1
2
3
4
5
6
7
var lat,lon,cityname=new Array;<br>function getLocation(){
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
这里首先判断下浏览器对于api的支持,如果不支持GG思密达咯。在调用


1
navigator.geolocation.getCurrentPosition这个函数后,我们会传入一个对象给showPosition这个函数,里面包含着经纬度
复制代码
function showPosition(position)
  {
  console.log(position.coords.latitude+"!!!"+position.coords.longitude)
    lat=position.coords.latitude;
    lon=position.coords.longitude;
    //alert(lat+"!!"+lon);
    
    // 百度地图API功能
    
    
    //alert(lat+"!!"+lon);
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(lon, lat);
    map.centerAndZoom(point, 15);
    
    var gc = new BMap.Geocoder();
    gc.getLocation(point, function(rs){
   var addComp = rs.addressComponents;
   console.log(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
    $(".location").html("你所在的城市:"+addComp.city);
    cityname=addComp.city.split("市");
    });
    
    var marker = new BMap.Marker(point);  // 创建标注
    map.addOverlay(marker);               // 将标注添加到地图中
    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
//天气模块
    weather();
    }
复制代码
console一下,方便看到我们得到的经纬度。有人会问,console只能在控制台看到啊,NONONO,Chrome inspect大家可以看下,支持手机端调试,博主有空也会稍微讲下inspect的,不过网上教程蛮多的,大家可以看看。实在不会,咱们可以alert对吧,哈哈......唔,扯偏了,咱们回到正题,得到经纬度后,咱们就需要调用baidu Map的api了,


传入咱们刚获取的lon,lat这时候就可以返回城市名等信息了。当然博主这边写的有点多,同时绘制了地图,大家可以适当删减。


ok,走到这步我们就可以开始伟大的旅行了,将我们拿到的cityname给我们可爱的Open weather提供的api
  • http://www.zaojuzi.com/minjiangushi/16448.html
    http://www.zaojuzi.com/minjiangushi/16447.html
    http://www.zaojuzi.com/minjiangushi/16446.html
    http://www.zaojuzi.com/yizhigushi/16435.html
    http://www.zaojuzi.com/yizhigushi/16434.html
    http://www.zaojuzi.com/yizhigushi/16433.html
    http://www.zaojuzi.com/yizhigushi/16437.html
    http://www.zaojuzi.com/minjiangushi/16445.html
    http://www.zaojuzi.com/minjiangushi/16444.html
    http://www.zaojuzi.com/minjiangushi/16443.html
    http://www.zaojuzi.com/minjiangushi/16442.html
    http://www.zaojuzi.com/minjiangushi/16441.html
    http://www.zaojuzi.com/dongwugushi/15969.html
    http://www.zaojuzi.com/dongwugushi/15968.html
    http://www.zaojuzi.com/dongwugushi/15967.html
    http://www.zaojuzi.com/dongwugushi/15966.html
    http://www.zaojuzi.com/dongwugushi/15965.html
    http://www.zaojuzi.com/dongwugushi/15964.html
    http://www.zaojuzi.com/dongwugushi/15963.html
    http://www.zaojuzi.com/dongwugushi/15962.html
    http://www.zaojuzi.com/dongwugushi/15961.html
    http://www.zaojuzi.com/dongwugushi/15750.html
    http://www.zaojuzi.com/dongwugushi/15749.html
    http://www.zaojuzi.com/chengyu/8804.html
    http://www.zaojuzi.com/chengyu/10766.html
    http://www.zaojuzi.com/chengyu/8876.html
    http://www.zaojuzi.com/yizhigushi/16440.html
    http://www.zaojuzi.com/yizhigushi/16439.html
    http://www.zaojuzi.com/yizhigushi/16438.html
    http://www.zaojuzi.com/yizhigushi/16436.html
    http://www.zaojuzi.com/yizhigushi/16432.html
    http://www.zaojuzi.com/yizhigushi/16431.html
    http://www.zaojuzi.com/yizhigushi/16430.html
    http://www.zaojuzi.com/yuyangushi/15500.html
    http://www.zaojuzi.com/yuyangushi/15499.html
    http://www.zaojuzi.com/yuyangushi/15498.html
    http://www.zaojuzi.com/yuyangushi/15497.html
    http://www.zaojuzi.com/yuyangushi/15496.html
    http://www.zaojuzi.com/yuyangushi/15495.html
    http://www.zaojuzi.com/yuyangushi/15494.html
    http://www.zaojuzi.com/yuyangushi/15493.html
    http://www.zaojuzi.com/yuyangushi/15492.html
    http://www.zaojuzi.com/tonghuagushi/16414.html
    http://www.zaojuzi.com/tonghuagushi/16413.html
    http://www.zaojuzi.com/tonghuagushi/16412.html
    http://www.zaojuzi.com/tonghuagushi/16411.html
    http://www.zaojuzi.com/tonghuagushi/16410.html
    http://www.zaojuzi.com/tonghuagushi/16409.html
    http://www.zaojuzi.com/tonghuagushi/16408.html
    http://www.zaojuzi.com/tonghuagushi/16407.html
    http://www.zaojuzi.com/tonghuagushi/16406.html
    http://www.zaojuzi.com/tonghuagushi/16405.html
    http://www.zaojuzi.com/tonghuagushi/16404.html
    http://www.zaojuzi.com/minjiangushi/16338.html
    http://www.zaojuzi.com/minjiangushi/16337.html
    http://www.zaojuzi.com/minjiangushi/16336.html
    http://www.zaojuzi.com/lishigushi/16391.html
    http://www.zaojuzi.com/lishigushi/16390.html
    http://www.zaojuzi.com/lishigushi/16389.html
    http://www.zaojuzi.com/lishigushi/16388.html
    http://www.zaojuzi.com/lishigushi/16387.html
    http://www.zaojuzi.com/lishigushi/16386.html
    http://www.zaojuzi.com/lishigushi/16385.html
    http://www.zaojuzi.com/lishigushi/16384.html
    http://www.zaojuzi.com/lishigushi/16383.html
    http://www.zaojuzi.com/lishigushi/16382.html
    http://www.zaojuzi.com/lishigushi/16381.html
    http://www.zaojuzi.com/kuazhangju/7357.html
    http://www.zaojuzi.com/shewenju/shewenju.html
    http://www.zaojuzi.com/paibiju/5294.html
    http://www.zaojuzi.com/content/47.html
    http://www.zaojuzi.com/content/16128.html
    http://www.zaojuzi.com/content/16127.html
    http://www.zaojuzi.com/content/16126.html
    http://www.zaojuzi.com/content/16125.html
    http://www.zaojuzi.com/content/16124.html
    http://www.zaojuzi.com/content/16123.html
    http://www.zaojuzi.com/content/16122.html
    http://www.zaojuzi.com/content/16121.html
    http://www.zaojuzi.com/content/16120.html
    http://www.zaojuzi.com/content/16119.html
    http://www.zaojuzi.com/content/16118.html
    http://www.zaojuzi.com/content/16117.html
    http://www.zaojuzi.com/content/16116.html
    http://www.zaojuzi.com/content/16115.html
    http://www.zaojuzi.com/content/16114.html
    http://www.zaojuzi.com/content/16113.html
    http://www.zaojuzi.com/content/16112.html
    http://www.zaojuzi.com/content/16111.html
    http://www.zaojuzi.com/content/16110.html
    http://www.zaojuzi.com/content/16109.html
    http://www.zaojuzi.com/content/16108.html
    http://www.zaojuzi.com/content/16107.html
    http://www.zaojuzi.com/content/16106.html
    http://www.zaojuzi.com/content/16105.html
    http://www.zaojuzi.com/content/16104.html
    http://www.zaojuzi.com/content/15944.html
    http://www.zaojuzi.com/content/15943.html
    http://www.zaojuzi.com/content/15942.html
    http://www.zaojuzi.com/content/15941.html
    http://www.zaojuzi.com/content/15940.html
    http://www.zaojuzi.com/content/15939.html
    http://www.zaojuzi.com/content/15938.html
    http://www.zaojuzi.com/content/15937.html
    http://www.zaojuzi.com/content/15936.html
    http://www.zaojuzi.com/content/page_3.html
    http://www.zaojuzi.com/content/page_4.html
    http://www.zaojuzi.com/content/page_5.html
    http://www.zaojuzi.com/content/page_6.html
    http://www.zaojuzi.com/content/page_7.html
    http://www.zaojuzi.com/content/page_67.html
    http://www.zaojuzi.com/nirenju/1519.html
    http://www.zaojuzi.com/nirenju/1304.html
    http://www.zaojuzi.com/nirenju/1216.html
    http://www.zaojuzi.com/nirenju/1150.html
    http://www.zaojuzi.com/nirenju/page_2.html
    http://www.zaojuzi.com/nirenju/page_3.html
    http://www.zaojuzi.com/nirenju/page_4.html
    http://www.zaojuzi.com/biyuju/3406.html
    http://www.zaojuzi.com/biyuju/3405.html
    http://www.zaojuzi.com/biyuju/3404.html
    http://www.zaojuzi.com/biyuju/8933.html
    http://www.zaojuzi.com/biyuju/8936.html
    http://www.zaojuzi.com/biyuju/8932.html
    http://www.zaojuzi.com/biyuju/663.html
    http://www.zaojuzi.com/biyuju/page_2.html
    http://www.zaojuzi.com/biyuju/page_3.html
    http://www.zaojuzi.com/biyuju/page_4.html
    http://www.zaojuzi.com/fanwenju/7135.html
    http://www.zaojuzi.com/fanwenju/7084.html
    http://www.zaojuzi.com/fanwenju/4834.html



    复制代码
    //查询天气
      function weather(){
         var url="http://api.openweathermap.org/data/2.5/weather?q="+cityname[0]+"&callback=msg&appid=openweather的accesskey";
         var scr=document.createElement('script');
         scr.setAttribute('type','text/javascript');
         scr.setAttribute('src',url);
         document.getElementsByTagName('head')[0].appendChild(scr);
        }
        //回调函数
      function msg(data){
        var local_weather=data.weather[0].main;
        var temp_max=parseInt(data.main.temp_max-273.13);
        var temp_min=parseInt(data.main.temp_min-273.13);
        $(".temp").html("温度:"+temp_min+"℃-"+temp_max+"℃");
        //alert("天气"+local_weather+"温度"+temp_min+"℃到"+temp_max+"℃");
    复制代码
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的百度Map accesskey"></script>
    利用JSONP给我们的数据,我们就可以拿到天气了,当然博主这里提醒一下,返回的数据最好在pc端console一下,返回的json还是蛮复杂的,还有一点就是一定要在head里面加上上面百度api的url哦


    当然最近听朋友说,百度提供了一个api市场,有更简单的天气api,大家有空可以去看看。互联网大牛不是吹的,哈哈。本来想贴上楼主的源码地址,但是楼主用的是sae,最近刚好赶上sae改革,流量贼贵,这里就只给大家提供一个思路了,有问题大家可以留言哦,看到的话,博主我会回复的
  • 你可能感兴趣的:(关于实现手机端自动获取天气的demo)