wex5 如何利用 百度地图 定位 和 天气插件
引包:
require("cordova!cordova-plugin-geolocation");
require("cordova!com.justep.cordova.plugin.baidulocation");
Model.prototype.modelLoad = function(event){
var self = this;
var data = self.comp("data1"); //获取节点
var gpsDtd = this.getLocation(); //调用自带定位插件
gpsDtd.done(function(position) {
var positioning=position.latitude+"|"+position.longitude; //获取经纬度
data.setValue("location", position.address); //获取位置
require([ 'http://api.map.baidu.com/getscript?v=1.4&ak=&services=&t=20170929031641' ], function() { //百度api 作用:通过经纬度获取城市
if(BMap){
var point = new BMap.Point(position.longitude,position.latitude);
var gc = new BMap.Geocoder();
gc.getLocation(point, function(rs){
var addComp = rs.addressComponents;
address = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
var params2 = {"city":encodeURI(addComp.city)};
Baas.sendRequest({ //访问后台天气接口
"url" : "/num/Data", // 请求地址
"action" : "weatherApi", // action
"params" : params2, // action对应的参数
"success" : function(weather){
console.info(weather);
if(weather && weather.status==200){
var forecast = weather.data.forecast[0];
var info = forecast.high +" "+ forecast.low +" "+ forecast.type +" "+ forecast.fx + forecast.fl ;
self.getElementByXid("weatherInfo").innerText=info;
}else{
self.getElementByXid("weatherInfo").innerText="暂无天气信息";
}
}
});
});
}
})
})
};
//定位方法
Model.prototype.getLocation = function() {
/*
* 获取当前定位,获取地理位置失败,暂时采用默认地址
*/
var gpsDtd = $.Deferred();
if (this.wxApi) {
this.wxApi.exec().done(function(wx) {
wx.getLocation({
type : "gcj02",
success : function(data) {
gpsDtd.resolve({
coorType : data.coorType,
address : data.address,
longitude : data.longitude,
latitude : data.latitude
});
},
cancel : function(res) {
gpsDtd.resolve({
longitude : 116.45764191999997,
latitude : 39.8622934399999
});
}
});
}).fail(function() {
gpsDtd.resolve({
longitude : 116.45764191999997,
latitude : 39.8622934399999
});
});
} else if (navigator.geolocation) {
var success = function(data) {
// data.longtitude 经度
// data.latitude 纬度
// data.address 文字描述的地址信息
// data.hasRadius 是否有定位精度半径
// data.radius 定位精度半径
// data.type 定位方式
// data.coorType
gpsDtd.resolve({
coorType : data.coorType,
address : data.address,
coorType: data.coorType,
longitude : data.coords.longitude,
latitude : data.coords.latitude
});
};
var fail = function(e) {
justep.Util.hint("获取地理位置失败,暂时采用默认地址");
gpsDtd.resolve({
longitude : 116.45764191999997,
latitude : 39.8622934399999
});
};
/**
* 如果在室内 enableHighAccuracy: true 反而误差更大
* 如果在室外 enableHighAccuracy: true 相对准确
*/
navigator.geolocation.getCurrentPosition(success, fail);
/*var gpsWatchID = navigator.geolocation.watchPosition(success, fail,{
timeout: 30*1000,
maximumAge: 30000,
enableHighAccuracy: true});*/
} else {
justep.Util.hint("获取地理位置失败,暂时采用默认地址");
gpsDtd.resolve({
longitude : 116.45764191999997,
latitude : 39.8622934399999
});
}
return gpsDtd.promise();
};
BAAS:
public static JSONObject weatherApi(JSONObject params, ActionContext context)
throws SQLException, NamingException, IOException {
Connection conn = context.getConnection(DATASOURCE_NUMYSQL);
String city = params.getString("city");
JSONObject rt = new JSONObject();
try {
// 把字符串转换为URL请求地址
URL url = new URL("http://www.sojson.com/open/api/weather/json.shtml?city="+city);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();// 连接会话
// 获取输入流
BufferedReader br = new BufferedReader(newInputStreamReader(connection.getInputStream(),"UTF-8"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
JSONObject weather = JSONObject.parseObject(sb.toString());
return weather;
}finally {
conn.close();
}
}
wex5 如何利用 百度地图 定位 和 天气插件的更多相关文章
- cordova百度地图定位Android版插件
本插件利用百度地图提供的定位功能进行Android版手机定位. 为什么没有iOS版? 因为iOS版有官方的定位插件cordova-plugin-geolocation可以使用. 请参照:cordova ...
- Android利用百度地图定位
百度地图照着百度的教程做的总是出现报错 请帮我看看错误在那 2013-12-13 15:16168海军 | 分类:百度地图 | 浏览1252次 java.lang.RuntimeException: ...
- 利用百度地图API,获取经纬度坐标
利用百度地图API,获取经纬度坐标 代码很简单,但在网上没找到现成的获取地图经纬度的页面. 就是想,给当前页面传递一个经纬度,自动定位到此经纬度.然后可以重新选择,选择完返回经纬度. 效果如下: 源代 ...
- Android 百度地图定位(手动+自动) 安卓开发教程
近由于项目需要,研究了下百度地图定位,他们提供的实例基本都是用监听器实现自动定位的.我想实现一种效果:当用户进入UI时,不定位,用户需要定位的时候,自己手动点击按钮,再去定位当前位置. 经过2天研究 ...
- 【百度地图API】建立全国银行位置查询系统(四)——如何利用百度地图的数据生成自己的标注
原文:[百度地图API]建立全国银行位置查询系统(四)--如何利用百度地图的数据生成自己的标注 摘要: 上一章留个悬念,"如果自己没有地理坐标的数据库,应该怎样制作银行的分布地图呢?&quo ...
- 百度地图定位SDK 之构想
百度地图定位 前提 从香港旅游回来,心中油然升起一股热血滂湃,激励自己发现市场需求,向创业奋进,朝着梦想前进. 简介 百度Android定位SDK支持Android1.5以及以上设备,提供: 定位功能 ...
- 利用百度地图API和群蚁算法,对TSP问题进行模拟与求解
前言 最近由于换了工作,期间也有反思和总结上家公司的得失,总觉得有什么事情当初可以完成或者完成得更好,其中TSP问题就是其中之一.当初在开发一个仓配系统的时候,有一个线路排程的需求,当时自己简单在纸上 ...
- Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”
今天老板让我验证一下技术可行性,记录下来. 需求 :定位手机的位置并在百度地图上显示,得到位置后使用前置摄像头进行抓拍 拿到这个需求后,对于摄像头的使用不太熟悉,于是我先做了定位手机并在百度地图上显示 ...
- IOS中使用百度地图定位后获取城市坐标,城市名称,城市编号信息
IOS中使用百度地图定位后获取城市坐标,城市名称,城市编号信息 /**当获取到定位的坐标后,回调函数*/ - (void)didUpdateBMKUserLocation:(BMKUserLocati ...
随机推荐
- docker-compose常用命令(持续更新...)
build 构建或重建服务 help 命令帮助 kill 杀掉容器 logs 显示容器的输出内容 port 打印绑定的开放端口 ps 显示容器 pull 拉取服务镜像 restart 重启服务 rm ...
- Spring Data概览
总结:JpaRepository继承PagingAndSortingRepository,PagingAndSortingRepository继承CrudRespository,CrudResposi ...
- 【转】HLS视频点播&直播初探
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- 有关ajax中的URL问题
url : ../../Service/MSD_Maintain.ashx/?action=Add4T2 url : Handler/MaintainHandler.ashx/?action= ...
- Why are dashes preferred for CSS selectors / HTML attributes?
Why are dashes preferred for CSS selectors / HTML attributes? I use dashes because I don't have to h ...
- 全面解读php-函数
一.静态变量 二.函数的参数 三.函数的引用返回 四.外部文件的导入 五.内置函数 1.时间日期函数 2.IP处理函数 3.打印函数: 分类 函数名 说明 语言结构 print() 只能打印一个变量 ...
- C++类继承方式及实践
直接上图: 以及: 实践如下: #include <iostream> using namespace std; class Father{ private: int father1; i ...
- LDA(Latent Dirichlet Allocation)主题模型算法
原文 LDA整体流程 先定义一些字母的含义: 文档集合D,topic集合T D中每个文档d看作一个单词序列< w1,w2,...,wn >,wi表示第i个单词,设d有n个单词.(LDA里面 ...
- Could not parse configuration: /hibernate.cfg.xml
hibernate需要联网验证dtd,错误原因:未联网或网速不行
- Activity 怎么和 Service 绑定,怎么在 Activity 中启动自己对应的 Service?
Activity 通过 bindService(Intent service, ServiceConnection conn, int flags)跟 Service 进行绑定,当绑定成功的时候 Se ...