前言:前面一段时间,公司项目里有一个需求 是获取当前手机地理位置当天的天气情况  将实时天气信息提供给客户。在网上搜索资料时候,发现知识很零碎,自己实现以后整理出来,方便于各位的学习与使用。

一、获取当前手机地理位置。

Html5提供接口plus.geolocation.getCurrentPosition(function(position) {

}, function(error) {});

可以用console.log(JSON.stringify(position));查看一下返回的内容,从中提取自己实际项目中需要地理信息。

在获取天气时候 为了优化用户体验。是需要做指引性质的操作的。例如 :百度地图的提示,是否开启手机精准定位等等。即需要检测当前用户是否已经开启了定位,如果没有,指引用户完成手机定位开启。如果已经开启手机定位则跳过。

                        var that = this;
if(plus.os.name == "Android") {
var context = plus.android.importClass("android.content.Context");
var locationManager = plus.android.importClass("android.location.LocationManager");
var mainplus = plus.android.runtimeMainActivity();
var mainSvr = mainplus.getSystemService(context.LOCATION_SERVICE);
var androidIsOpen = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
if(!androidIsOpen) {
mui.confirm('开启位置服务,获取精准定位', ' ', ['去开启', '取消'], function(result) {
if(result.index == 0) {
var Intent = plus.android.importClass('android.content.Intent');
var Settings = plus.android.importClass('android.provider.Settings');
var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); //可设置表中所有Action字段
mainplus.startActivity(intent);
}
}, 'div')
}
that.IsOpenGps = androidIsOpen;
} else {
var CLLocationManager = plus.ios.import("CLLocationManager");
var authorizationStatus = CLLocationManager.authorizationStatus();
switch(authorizationStatus) {
case 0:
mui.toast("未被授权位置信息,无法展示天气");
/// User has not yet made a choice with regards to this application未作出位置信息权限选择
break;
case 1:
mui.toast("未被授权位置信息,无法展示天气");
// This application is not authorized to use location services. Due
// to active restrictions on location services, the user cannot change
// this status, and may not have personally denied authorization未被授权位置信息
break;
case 2:
mui.toast("未被授权位置信息,无法展示天气");
// User has explicitly denied authorization for this application, or
// location services are disabled in Settings.明确拒绝授权位置信息
break;
case 3:
//mui.toast("已经获取位置信息");
// User has granted authorization to use their location at any time,
// including monitoring for regions, visits, or significant location changes.
break;
case 4:
//mui.toast("已经获取位置信息");
// User has granted authorization to use their location only when your app
// is visible to them (it will be made visible to them if you continue to
// receive location updates while in the background). Authorization to use
// launch APIs has not been granted.
break;
case 5:
mui.toast("未被授权位置信息,无法展示天气");
// This value is deprecated, but was equivalent to the new -Always value.值被弃用
break;
defalut:
break;
}
var isIosOpen = false;
if(authorizationStatus == 3 || authorizationStatus == 4) {
isIosOpen = true;
}
that.IsOpenGps = isIosOpen;

二、根据获取的地理信息获取当天的天气情况。

这里采用了百度天气的接口。

1.首先需要百度地图的API Key   申请地址:http://lbsyun.baidu.com/apiconsole/key

2.创建应用    选择微信小程序的应用类型。

3.获取天气JSON

var baidukey = "voUKGmLO***********Ia1qQc";//申请的AK

//LatandLong  经纬度
function GetTodayWeather(city, LatandLong,callback) {
var jsonUrl = "http://api.map.baidu.com/telematics/v3/weather?location=" + LatandLong + "&output=json&ak=" + baidukey + "";
mui.ajax(jsonUrl, {
dataType: 'json',
type: 'get',
//timeout: 10000,
success: function(data) {
if(data.status == "success") {
callback(data.results[0])
}
},
error: function(xhr, type, error) {
//console.log(type)
mui.toast("网络异常,请稍候再试");
}
});
}

4.天气JSON

{
error: 0,
status: "success",
date: "2013-07-17",
results: [
{
currentCity: "北京市",
weather_data: [
{
date: "今天(周三)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "微风",
temperature: "23℃"
},
{
date: "明天(周四)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/leizhenyu.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/zhongyu.png",
weather: "雷阵雨转中雨",
wind: "微风",
temperature: "29~22℃"
},
{
date: "后天(周五)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/yin.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "阴转多云",
wind: "微风",
temperature: "31~23℃"
},
{
date: "大后天(周六)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "微风",
temperature: "31~24℃"
}
]
},
{
currentCity: "合肥市",
weather_data: [
{
date: "今天(周三)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东风3-4级",
temperature: "27℃"
},
{
date: "明天(周四)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东北风3-4级",
temperature: "35~27℃"
},
{
date: "后天(周五)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "南风",
temperature: "35~27℃"
},
{
date: "大后天(周六)",
dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png",
nightPictureUrl: "http://api.map.baidu.com/images/weather/night/duoyun.png",
weather: "多云",
wind: "东风",
temperature: "34~27℃"
}
]
}
]
}

5.在回调函数里面解析自己需要的数据即可。

结语:希望对正在用Hbuilder 开发APP的你 有所帮助。

最新文章

  1. kafka模块概述
  2. Day4_计算器
  3. iOS:UITableView 方法 属性
  4. XML详解:第一部分
  5. SharePoint安全 - SharePoint网站常用页面URL索引
  6. 使用Genymotion作Android开发模拟器:安装Genymotion、部署Genymotion Vitrue Device、安装Genymotion eclipse插件
  7. Ural 1519. Formula 1 优美的插头DP
  8. C语言生产随机数的方法
  9. HBASE学习笔记--shell
  10. SAP HANA开发中常见问题- 基于SAP HANA平台的多团队产品研发
  11. R语言包下载(转载)
  12. for或者while的标记循环
  13. Cutterman - 最好用的切图工具
  14. 3150luogu洛谷
  15. maven搭建ssh项目及遇到的问题
  16. 【err】VIDEOIO ERROR: V4L: index 0 is not correct!Unable to connect to camera
  17. OCR学习及tesseract的一些测试
  18. WannaCry病毒应急处置
  19. Android Studio 独立引入(非友盟)微博分享和回调时问题
  20. perl 列出一个目录下的文件的大小

热门文章

  1. 扫描仪文字识别ORC软件加强版(文通慧视完整版)下载
  2. python安装wordcloud库报错
  3. Resharper 2020 免费破解版
  4. windows快速安装linux虚拟机
  5. 备份etc下的内容
  6. Unity 打AssetBundle和加载方案
  7. JVM内存区域以及各区域的内存溢出异常,内存分代策略,垃圾收集算法,各种垃圾收集器
  8. Java中的IO操作和缓冲区
  9. 跟着尚硅谷系统学习Docker-【day07】
  10. python实践项目1