Hbuilder获取手机当前地理位置的天气
前言:前面一段时间,公司项目里有一个需求 是获取当前手机地理位置当天的天气情况 将实时天气信息提供给客户。在网上搜索资料时候,发现知识很零碎,自己实现以后整理出来,方便于各位的学习与使用。
一、获取当前手机地理位置。
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的你 有所帮助。
Hbuilder获取手机当前地理位置的天气的更多相关文章
- 用百度SDK获取地理位置和天气信息
以下实现通过百度SDK获取地理位置和天气信息,请參考title=android-locsdk/guide/v5-0">百度开发文档 1. 在相关下载最新的库文件.将so文件的压缩文件解 ...
- iOS 整理笔记 获取手机信息(UIDevice、NSBundle、NSLocale)
/* iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发ap ...
- 使用navigator.geolocation来获取用户的地理位置信息
使用navigator.geolocation来获取用户的地理位置信息 W3C 中新添加了一个名为 Geolocation的 API 规范,Geoloaction API的作用就是通过浏览器获取用户的 ...
- iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)
iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到 ...
- ios 获取手机信息(UIDevice、NSBundle、NSLocale)
iOS的SDK中提供了UIDevice.NSBundle,NSLocale. UIDevice UIDevice提供了多种属性.类函数及状态通知,帮助我们全方位了解设备状况. 从检測电池 ...
- 如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介
如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介 一.总结 一句话总结:Geolocation API(地理位置应用程序接口)提供了一个可以准确知道浏览器用户当前位置的方法 ...
- 利用RxJava获取手机已安装的App的图片、应用名称和版本号
先上效果图: 获取手机已安装的App列表利用Android系统API就可以办到,这里为什么要引入RxJava?现在我们假设一下有下面几个需求: 1.我们不需要所有的App,只需要用户安装的第三方App ...
- iOS获取手机型号,类似iphone 7这种 含swift和OC
获取手机设备信息,如name.model.version等,但如果想获取具体的手机型号,如iphone5.5s这种,就需要如下这种 swift: func phonetype () -> Str ...
- UI Automator Viewer获取手机镜像时报错
使用UI Automator Viewer获取手机镜像时报错,具体信息如下: Error while obtaining UI hierarchy XML file: com.android.ddml ...
随机推荐
- 有了MDL锁视图,业务死锁从此一目了然
摘要:MDL锁视图让一线运维人员清晰地查看数据库各session持有和等待的元数据锁信息,从而找出数据库MDL锁等待的根因,准确地进行下一步决策. 当多用户共同存取数据时,数据库中就会产生多个事务同时 ...
- opencv 截图并保存
opencv 截图并保存(转载) 代码功能:选择图像中矩形区,按S键截图并保存,Q键退出. #include<opencv2/opencv.hpp> #include<iostrea ...
- 项目介入EF Core
目前.Net主流的ORM有SqlSugar.Entity Framework.Dapper,其它的我就不列举了.其实和Java那边ibatis相比,他们都比较轻量.之前用ibatis开发,真的很麻烦, ...
- 深入了解v-model流程
v-model原理 vue中v-model是一个语法糖,所谓的语法糖就是对其他基础功能的二次封装而产生的功能.简单点说,v-model本身就是父组件对子组件状态以及状态改变事件的封装.其实现原理上分为 ...
- android studio 如何进行格式化代码 快捷键必备
在Eclipse中,我们一般使用Ctrl+Shift+F来格式化代码,Android Studio中需要换成: Reformat code CTRL + ALT + L (Win) OPTION + ...
- Google Analytics谷歌分析事件之非互动事件
非互动事件官方的解释如下 “非互动”一词是指可选的布尔值参数,此参数可以传递到用于发送事件命中的方法.通过此参数,您可以确定要如何为网站上包含事件衡量的网页定义跳出率.例如,假设您的首页上内嵌有一个视 ...
- ASP调用WEBSERVICE并对返回结果进行解析时遇到的问题
项目上用动易平台做新闻发布网站,动易平台是用ASP做的,期间需要根据当前登录的用户,取其他系统比如OA的待办事项进行列表展示,OA组的同事给了我一个WSDL接口,百度了很多ASP调用webservic ...
- Java中构建长字符串的四种模式
回字有四种写法,构建字符串也有四种方式. 用+号最快,第二第三种可读性好,第四种是log4j自有的.下面请见代码: package logbackCfg; import java.text.Messa ...
- 同样是logback1.11,更换了log配置后,无论是否有线程持续不断写入log文件,log文件会按设定以日期序号轮换
上次发现了logback1.11的一个bug,即有线程持续写入log,则log文件不会按设定模式进行轮换. 但发现同样采用logback1.11的另外一个工程,它的日志文件就没有错误,于是参照其配置文 ...
- 自定义AQS独占模式下的同步器来实现独享锁
自定义AQS独占模式下的同步器来实现独享锁 /** * 自定义AQS独占模式下的同步器来实现独享锁 */ public class Mutex implements Lock, java.io.Ser ...