1***@qq.com
1***@qq.com
  • 发布:2020-05-29 15:53
  • 更新:2023-02-09 14:14
  • 阅读:9927

uniApp怎么实时的获取当前的位置,大部分的应用会在后台被杀死!

分类:uni-app
1. 用uni自带的getlocation获取位置  
var time =setInterval(function() {  
            if (uni.getStorageSync('token') || 0) {  
                uni.getLocation({  
                    type: 'gcj02 ',  
                    success: function(res) {  
                        uni.setStorageSync('userLocation', JSON.stringify(res));  
                        that.request({  
                            url: '/driver/order/point_upload',  
                            data: {  
                                location: res.longitude + ',' + res.latitude,  
                                locatetime: new Date().getTime() || '',  
                                speed: res.speed || '',  
                                accuracy: res.accuracy || ''  
                            },  
                            method: 'post'  
                        }).then(res => {  
                            uni.showToast({  
                                title: res.msg,  
                                icon: 'none'  
                            });  
                        });  
                    },  
                    fail: function() {  
                        uni.showToast({  
                            title: '获取位置失败',  
                            icon: 'none'  
                        });  
                    }  
                });  
            }  
        }, 3000);  
2.用高德地图的js包的方法获取位置  
var time = setInterval(function() {  
            if (uni.getStorageSync('token') || 0) {  
                that.Amap.getRegeo({  
                    success: data => {  
                        //console.log(data[0].latitude, data[0].longitude);  
                        that.request({  
                            url: '/driver/order/point_upload',  
                            data: {  
                                location: data[0].longitude + ',' + data[0].latitude,  
                                locatetime: new Date().getTime()  
                            },  
                            method: 'post'  
                        }).then(res => {  
                            uni.showToast({  
                                title: res.msg + 'getRegeo',  
                                icon: 'none'  
                            });  
                        });  
                    }  
                });  
            }  
        }, 3000);  
3.用plus位置读取的方法获取  
var time = setInterval(function() {  
            if (uni.getStorageSync('token') || 0) {  
                plus.geolocation.getCurrentPosition(  
                    function(position) {  
                        that.request({  
                            url: '/driver/order/point_upload',  
                            data: {  
                                location: position.coords.longitude + ',' + position.coords.latitude,  
                                locatetime: new Date().getTime(),  
                                speed: position.coords.speed || '',  
                                accuracy: position.coords.accuracy || '',  
                                height: position.coords.altitude || ''  
                            },  
                            method: 'post'  
                        }).then(res => {  
                            uni.showToast({  
                                title: res.msg + 'getCurrentPosition',  
                                icon: 'none'  
                            });  
                        });  
                    },  
                    function(e) {  
                        uni.showToast({  
                            title: e.message,  
                            icon: 'none'  
                        });  
                    },  
                    { geocode: false, coordsType: 'amap', timeout: 2900 }  
                );  
            }  
        }, 3000);  
4.还是用plus的位置监听的方法  
plus.geolocation.watchPosition(  
            function(position) {  
                if (uni.getStorageSync('token') || 0) {  
                    that.request({  
                        url: '/driver/order/point_upload',  
                        data: {  
                            location: position.coords.longitude + ',' + position.coords.latitude,  
                            locatetime: new Date().getTime(),  
                            speed: position.coords.speed || '',  
                            accuracy: position.coords.accuracy || '',  
                            height: position.coords.altitude || ''  
                        },  
                        method: 'post'  
                    }).then(res => {  
                        uni.showToast({  
                            title: res.msg + 'watchPosition',  
                            icon: 'none'  
                        });  
                    });  
                }  
            },  
            function(e) {  
                uni.showToast({  
                    title: e.message,  
                    icon: 'none'  
                });  
            },  
            {  
                provider: 'amap',  
                //是否使用高精度设备,如GPS。默认是true  
                enableHighAccuracy: true,  
                //超时时间,单位毫秒,默认为0  
                //使用设置时间内的缓存数据,单位毫秒  
                //默认为0,即始终请求新数据  
                //如设为Infinity,则始终使用缓存数据  
                maximumAge: 3000  
            }  
        );

最后发现,特么的位置获取奇怪的一批感觉被位置欺骗了
很多人说挂后台会被杀死,你可以试着把应用的权限开启,主要是手机为了省电会把应用关掉,你们可以在设置里面把这个权限开启后台运行

2020-05-29 15:53 负责人:无 分享
已邀请:
walker9277

walker9277

自己开发一个原生插件去获取定位(获取去插件市场搜索(原生插件),里面有一个定位),这样定位就不会偏差很大,然后根据定位去设置前台固定(高德地图)(这样就可以延长后台存活时间(五六个小时))

  • 1***@qq.com (作者)

    很多人问我咋解决的,我最后是用插件市场的,原生安卓定位插件解决的。但是还是存在安卓高版本杀死问题!

    2020-12-02 09:22

1***@qq.com

1***@qq.com (作者)

好无聊啊,想睡觉。想不到解决方案了。

  • FE_liyangyang

    您好,方便咨询您点问题吗?

    2020-08-17 10:41

1***@qq.com

1***@qq.com (作者)

待会试试小程序可不可以实时上报


//获取应用实例  
const app = getApp()  

var point = [];  
var that2;  

function drawline() {  
  that2.setData({  
    polyline: [{  
      points: point,  
      color: '#99FF00',  
      width: 4,  
      dottedLine: false  
    }]  
  });  
}  

//获取经纬度  
function getlocation() {  
  var lat, lng;  
  wx.getLocation({  
    type: 'gcj02',  
    success: function(res) {  
      lat = res.latitude;  
      lng = res.longitude;  
      point.push({  
        latitude: lat,  
        longitude: lng  
      });  
      console.log(point);  
    }  
  });  
}  

  Page({  
    data: {  
      polyline: [],  
      longitude:"",  
      latitude:""  
    },  
    //事件处理函数  
    bindViewTap: function() {  
      wx.navigateTo({  
        url: '../logs/logs'  
      })  
    },  
    onLoad: function() {  
     that2 = this;  
      wx.getLocation({  
        type: 'gcj02',  
        success: function(res) {  
          that2.setData({  
            longitude: res.longitude,  
            latitude: res.latitude,  
          });  
        }  
      });  
    },  
     start: function () {  
      this.timer = setInterval(repeat, 1000);  
      function repeat() {  
        console.log('start');  
        getlocation();  
        drawline();  
      }  
    },  
    end: function () {  
      console.log('end');  
      clearInterval(this.timer);  
    }  
  });  
1***@qq.com

1***@qq.com (作者)

我发现还是获取定位不准确的问题,或者说是getlocation无法在后台运行的可能。目前已经解决在后台运行,现在的困扰是定位不准确,一直停留在手机息屏那一瞬间的位置,你可以理解为,在一瞬间你消失了。目前还是没有找到解决方案。

  • 1***@qq.com

    如果让它在后台也能运行

    2021-01-28 10:08

7***@qq.com

7***@qq.com

作者解决了没有

  • 李小伦

    老铁 有解决方案没有

    2020-09-25 10:14

  • 1***@qq.com (作者)

    回复 李小伦: 用的是插件市场的定位插件,安卓高版本还是存在问题

    2020-10-30 15:28

2***@qq.com

2***@qq.com

https://ext.dcloud.net.cn/plugin?id=3422

1***@qq.com

1***@qq.com

请问作者,这个问题最后怎么解决的。移动速度能获取到嘛

要回复问题请先登录注册