引包:
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();
}
}