当前位置:网站首页>微信小程序startLocationUpdateBackground()简单实现骑手配送位置
微信小程序startLocationUpdateBackground()简单实现骑手配送位置
2022-06-23 16:50:00 【亚历山大海】
注意:以下效果仅在真机中生效
骑手中心点会跟随当前的位置变化而变化
<view class="map_box">
<map id='myMap' scale="15" max-scale="15" min-scale="3" latitude="{
{markers[0].latitude}}" longitude="{
{markers[0].longitude}}" markers="{
{markers}}">
</map>
</view>
<view class="weizhixuan_box flex column">
<view class="itme flex acenter" catchtap="weizhiXinxi" data-idx="0">选择起点:{
{markers[0].longitude}};{
{markers[0].latitude}}</view>
<view class="itme flex acenter" catchtap="weizhiXinxi" data-idx="1">选择终点:{
{markers[1].longitude}};{
{markers[1].latitude}}</view>
<view class="annui flex center" catchtap="GOkaishi">开始配送</view>
</view>
距离:{
{julikm}}KMvar util = require("../utils/util.js");
var app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
timer: '',
markers: [],
julikm: 0,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
var that = this;
wx.startLocationUpdateBackground({
success(res) {
console.log('开启后台定位', res);
},
fail(res) {
console.log('开启后台定位失败', res);
//授权失败后引导用户打开定位信息
wx.getSetting({
success: function (res) {
var statu = res.authSetting;
console.log(statu)
if (!statu["scope.userLocationBackground"]) {
wx.showModal({
title: "是否授权使用期间和离开后!",
content: "需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
success: function (tip) {
if (tip.confirm) {
wx.openSetting({
success: function (data) {
if (data.authSetting["scope.userLocationBackground"] === true) {
}
}
});
} else {
//console.log('用户拒绝打开设置界面')
}
}
});
}
}
});
}
});
},
//监听地理位置变化
mapWeizhi: function () {
var that = this;
const _locationChangeFn = function (res) {
var markers = that.data.markers;
markers[0].latitude = res.latitude;
markers[0].longitude = res.longitude;
that.setData({
markers,
})
that.GetDistance();
wx.offLocationChange(_locationChangeFn)
that.mapWeizhi2()
}
wx.onLocationChange(_locationChangeFn)
},
//监听地理位置变化
mapWeizhi2: function () {
var that = this;
//定时执行每隔40秒获取当前位置(配送员)一次
that.timer = setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
const _locationChangeFn = function (res) {
var markers = that.data.markers;
markers[0].latitude = res.latitude;
markers[0].longitude = res.longitude;
that.setData({
markers,
})
that.GetDistance();
wx.offLocationChange(_locationChangeFn)
}
wx.onLocationChange(_locationChangeFn)
}, 5000)
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
var that = this;
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
var that = this;
},
//util统一跳转
onUrls: function (event) {
util.onUrls(event);
},
// 拉起地图选点
weizhiXinxi: function (e) {
var that = this;
var idx = e.currentTarget.dataset.idx;
var markers = that.data.markers;
wx.chooseLocation({ //这个是拉起搜索
latitude: that.data.tempMapIpInfo.latitude,
longitude: that.data.tempMapIpInfo.longitude,
success(res) {
// console.log(res)
markers[idx] = {};
markers[idx].height = 25;
markers[idx].width = 25;
markers[idx].iconPath = idx > 0 ? '/pages/images/ces/wode.png' : '/pages/images/ces/qishou.png';
markers[idx].id = idx;
markers[idx].longitude = res.longitude;
markers[idx].latitude = res.latitude;
markers[idx].title = idx ? '终点' : '起点';
that.setData({
markers,
})
},
})
},
//开始配送
GOkaishi: function (e) {
var that = this;
that.mapWeizhi();
},
//计算两点之家的距离
GetDistance: function (e) {
var that = this;
var markers = that.data.markers;
that.setData({
julikm: util.GetDistance(markers[0].latitude, markers[0].longitude, markers[1].latitude, markers[1].longitude),
})
}
}).map_box {
width: 100%;
height: 600rpx;
}
.map_box #myMap {
width: 100%;
height: 100%;
}
.weizhixuan_box {
margin: 20rpx;
}
.weizhixuan_box .itme {
height: 80rpx;
}
.weizhixuan_box .annui {
background-color: #f00;
color: #fff;
height: 100rpx;
margin-top: 20rpx;
}
JS计算两点之间的距离参考文章:https://blog.csdn.net/qq_31156277/article/details/81712226
边栏推荐
- MySQL - reasons for using repeatable read
- New function! Qianfan magic pen apaas December capability monthly report
- 一文读懂麦克风典型应用电路
- What if the website is poisoned
- Hapoxy-集群服务搭建
- AMQP protocol
- MySQL transaction and its characteristics and locking mechanism
- 论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints
- Lighthouse open source application practice: o2oa
- JS reset form
猜你喜欢

论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks

Date selection of hotel check-in time and check-out time

论文阅读 (56):Mutli-features Predction of Protein Translational Modification Sites (任务)

论文阅读 (55):Dynamic Multi-Robot Task Allocation under Uncertainty and Temporal Constraints

论文阅读 (48):A Library of Optimization Algorithms for Organizational Design

论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...

CRMEB 二开短信功能教程

JSON - learning notes (message converter, etc.)
![[30. concatenate substrings of all words]](/img/e7/453c8524a23fbb7501e85140547ce1.png)
[30. concatenate substrings of all words]

全局组织结构控制之抢滩登陆
随机推荐
[mae]masked autoencoders mask self encoder
Ner's past, present and future Overview - Future
手机开户流程是什么?现在网上开户安全么?
Mobile SSH connection tool
Year end: the "time value" and business methodology of live broadcast E-commerce
如何通过线上股票开户?在线开户安全么?
Kerberoasting without SPN
How to use JSON data format
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
酒店入住时间和离店时间的日期选择
Tencent three sides: how to duplicate 4billion QQ numbers?
January 5, 2022: there are four kinds of rhythms: AABB, ABAB and ABB
论文阅读 (53):Universal Adversarial Perturbations
How code 39 check bits are calculated
Postgresql_根据执行计划优化SQL
This time, thoroughly understand the SparseArray implementation principle
Explanation of the principle and code implementation analysis of rainbow docking istio
New function! Qianfan magic pen apaas December capability monthly report
Wechat applet: time selector for the estimated arrival date of the hotel
Self taught programming introduction, what language to learn first?