|
楼主 |
发表于 2016-8-5 11:30:10
|
显示全部楼层
我贴下代码:
home.js的modelOnLoad事件中:
- define(function(require) {
- var $ = require("jquery");
- var justep = require("$UI/system/lib/justep");
- var fluxUtil = require("$UI/fluxJsTools/fluxAppUtil");// 工具类
- require("cordova!cordova-plugin-spinner-dialog");
- require("cordova!de.appplant.cordova.plugin.background-mode");
- Model.prototype.modelLoad = function(event) {
- var time1 = "";
- document.addEventListener('deviceready', function() {
- /** 自动激活后台运行服务 */
- cordova.plugins.backgroundMode.enable();
- /** 当后台运行服务激活时执行的方法 */
- cordova.plugins.backgroundMode.onactivate = function() {
- timer = setInterval(function() {
- fluxUtil.addPosition();
- }, 60*1000);
- };
- cordova.plugins.backgroundMode.ondeactivate = function() {
- clearInterval(timer);
- };
- });
- };
- return Model;
- });
复制代码
工具类(fluxAppUtil.js):
- define(function(require) {
- var $ = require("jquery");
- var justep = require("$UI/system/lib/justep");
- require("$UI/system/lib/cordova/cordova");
- require("cordova!cordova-plugin-geolocation");
- var province = "";
- var serParamsUrl = "";
- var fluxUtil = {
- addPosition : function() {
- var province = "";// 省
- var city = "";// 市
- var section = "";// 区
- var street = "";// 街道
- var location = "";// 当前地址
- var addComp = "";
- var pPoint = "";
- navigator.geolocation.getCurrentPosition(success, error);
- function success(position) {
- var longitude = position.coords.longitude;// 经度
- var latitude = position.coords.latitude;// 纬度
- var gpsPoint = new BMap.Point(longitude, latitude);
- // 原始坐标转换为百度坐标(一定要等待地图加载完成之后才能进行)
- var convertor = new BMap.Convertor();
- var pointArr = [];
- pointArr.push(gpsPoint);
- convertor.translate(pointArr, 1, 5, translateCallback);// 开始转换坐标
- // 坐标转换完之后的回调函数
- function translateCallback(data) {
- var lng = data.points[0].lng;
- var lat = data.points[0].lat;
- var newPoint = new BMap.Point(lng, lat);// 生成点
- var geoc = new BMap.Geocoder();// 获取位置对象
- if (localStorage.getItem("platfrom") === "ios") {
- pPoint = newPoint;
- } else {
- pPoint = gpsPoint;
- }
- geoc.getLocation(pPoint, function(rs) {// 地址逆解析
- var userId = localStorage.getItem("username");
- addComp = rs.addressComponents;
- province = addComp.province;
- city = addComp.city;
- section = addComp.district;
- street = addComp.street;// addComp.streetNumber
- location = rs.address;
- justep.Baas.sendRequest({
- "url" : "/jinxdd/map",
- "action" : "getPosition",
- "params" : {
- "province" : province,
- "city" : city,
- "longitude" : longitude,
- "latitude" : latitude,
- "latitude_transfered" : lat,
- "longitude_transfered" : lng,
- "section" : section,
- "street" : street,
- "location" : location,
- "userId" : userId,
- "action_type" : "GPS_TRACKING",// 在途跟踪
- "addWho" : userId
- },
- "success" : function(data) {
- console.log("tms_gps_tracking类型位置上传成功!");
- position = "";// 上传成功后清除position的缓存
- },
- "error" : function(err) {
- }
- });
- });
- }
- }
- function error(error) {
- switch (error.code) {
- case error.PERMISSION_DENIED:
- alert("未授予获取定位权限!");
- break;
- case error.PERMISSION_UNAVAILABLE:
- alert("位置信息无效!");
- break;
- case error.TIMEOUT:
- alert("获取定位信息超时,请重试!");
- break;
- case error.UNKOWN_ERROR:
- alert("发生位未知错误!");
- break;
- }
- }
- }
- };
- return fluxUtil;
- });
复制代码
|
|