当前位置:网站首页>微信小程序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
边栏推荐
- JS reset form
- B. Integers Shop-Hello 2022
- How code 39 check bits are calculated
- A set of code to launch seven golang web frameworks at the same time
- Date selection of hotel check-in time and check-out time
- 对抗攻击与防御 (2):对抗样本的反制策略
- Li Kou daily question - day 25 -495 Timo attack
- Installation, configuration, désinstallation de MySQL
- Skills that all applet developers should know: applying applet components
- Explanation of the principle and code implementation analysis of rainbow docking istio
猜你喜欢

Wechat applet: time selector for the estimated arrival date of the hotel

对抗攻击与防御 (1):图像领域的对抗样本生成

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

Performance test bottleneck tuning in 10 minutes! If you want to enter a large factory, you must know

微信小程序:酒店预计到店日期的时间选择器

美团三面:聊聊你理解的Redis主从复制原理?

Date selection of hotel check-in time and check-out time
![[30. concatenate substrings of all words]](/img/e7/453c8524a23fbb7501e85140547ce1.png)
[30. concatenate substrings of all words]

Alien world, real presentation, how does the alien version of Pokemon go achieve?

全局组织结构控制之抢滩登陆
随机推荐
[JS reverse hundred examples] pedata encryption information and zlib Application of gunzipsync()
Go unit test
12 initialization of beautifulsoup class
How to make a shirt certificate
论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
10分钟后性能测试瓶颈调优!想进大厂这个必须会
Intelligent supply chain collaborative management solution for logistics industry
. Net cloud native architect training camp (responsibility chain mode) -- learning notes
JSON - learning notes (message converter, etc.)
全局组织结构控制之抢滩登陆
Hands on data analysis unit 2 section 4 data visualization
MySQL的 安装、配置、卸载
手机开户一般哪个证券公司好?在线开户安全么?
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
Goframe framework: basic auth Middleware
Alien world, real presentation, how does the alien version of Pokemon go achieve?
C. Set or Decrease-Educational Codeforces Round 120 (Rated for Div. 2)
Interpretation of eventbus source code
Crmeb second open SMS function tutorial
How to make validity table