正在做一个小项目,用到定位和天气功能,但又不想太麻烦,而且只是个辅助功能而已,就不想话太多时间在上面,但又找不到好的方法,网上都是说什么去注册然后获取key之类的,因为练手项目,不想走那些过程,而且有的天气API还收费,用起来实在不太方便,找了许久。终于找到了一个可以用的key,自己就可以省去很多麻烦,然后照着接口网站格式填入数据自己就可以使用了,好东西必须分享,希望对大家有帮助:
根据地址查询天气:
http://api.map.baidu.com/telematics/v3/weather?location=南江&output=json&ak=9cCAXQFB468dsH11GOWL8Lx4
返回结果:
{"error":0,"status":"success","date":"2016-12-08","results":[{"currentCity":"南江","pm25":"90","index":[{"title":"穿衣","zs":"较冷","tipt":"穿衣指数","des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,温度适宜,是个好天气哦。这样的天气适宜旅游,您可以尽情地享受大自然的风光。"},{"title":"感冒","zs":"易发","tipt":"感冒指数","des":"昼夜温差很大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"天气较好,但考虑天气寒冷,推荐您进行各种室内运动,若在户外运动请注意保暖并做好准备活动。"},{"title":"紫外线强度","zs":"中等","tipt":"紫外线强度指数","des":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}],"weather_data":[{"date":"周四 12月08日 (实时:15℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"微风","temperature":"15 ~ 4℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"阴","wind":"微风","temperature":"13 ~ 6℃"},{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"阴转小雨","wind":"微风","temperature":"12 ~ 8℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"微风","temperature":"11 ~ 9℃"}]}]}
相信又有人说:那还不是要获取地址名称,还有定位,还不是用到地图的API。太麻烦了。不要着急,继续往下看:
获取地址名称:
返回结果:
renderReverse&&renderReverse({"status":0,"result":{"location":{"lng":118.81473329999992,"lat":32.06998332332286},"formatted_address":"江苏省南京市玄武区内环东线","business":"玄武湖,火车站,太平门","addressComponent":{"country":"中国","country_code":0,"province":"江苏省","city":"南京市","district":"玄武区","adcode":"320102","street":"内环东线","street_number":"","direction":"","distance":""},"pois":[],"poiRegions":[{"direction_desc":"内","name":"玄武湖公园","tag":"旅游景点"}],"sematic_description":"玄武湖公园内,听秋亭东北502米","cityCode":315}})
看起来很熟悉吧,没错,和上面的开头一样,都是百度地图的API,所以后面的key也可以通用,哈哈,这下好了。
首先,这是一个逆地理编码的调用接口,就是说可以根据你的经纬度获取你的地址的名称,经纬度就可以用手机自带的GPS或者网络基站定位获取,这下就没有问题了,只需要填入数据然后访问接口就可以获取到数据,然后自己再解析json数据显示更新到你的布局上就完了,哈哈,是不是很容易!
所以感觉网络还是很强大滴,可以省去很多麻烦的步骤,所以学会合理使用网络资源是非常重要的。
今天这篇文章就到这里了,喜欢的朋友可以点个赞,谢谢!O(∩_∩)O