当前位置:网站首页>Wechat applet realizes map navigation + door-to-door recycling
Wechat applet realizes map navigation + door-to-door recycling
2022-06-30 14:11:00 【Ebony feed feed Wayne yetun wyuwenmu Bian OSAS】
List of articles
Preface
Use the wechat applet to get the map And related development
Tips : The following is the main body of this article , The following cases can be used for reference
One 、 What needs to be prepared ?
If you use the map in the wechat applet You have to go to the official website of the app to apply for maps Key This can be used in subsequent operations key To navigate
Two 、 Use steps
1. Introduce packaged JS Code
JavaScript The code is as follows ( Example ):
export default class Http{
request(url,data=[],method='get'){
var header={}
var token=wx.getStorageSync('token')
if(token){
header['Authization']=token
}
var openid=wx.getStorageSync('openid')
if(openid){
header['openid']=openid
}
var url='http://www.test.com/'+url // Fill in your own interface address here
return new Promise(function(resove,ret){
wx.request({
url,
data,
method,
header,
success:function(res){
if(res.data=='token invalid '){
wx.redirectTo({
url: '/pages/login/login',
})
}
resove(res.data)
},
fail:function(ret){
wx.showToast({
title: ' request timeout , Please try again later ',
icon:'error',
duration:3000
})
}
})
})
}
}app.js Well sealed in the middle wx.login Function of
Before using the map, you need to The current geographical location is set Use wx.getLocation Methods
// app.js
import Http from "./utils/http";
let http=new Http()
App({
onLaunch() {
var openid=wx.getStorageSync('openid')
if(!openid){
// Sign in
wx.login({
success: res => {
var data={code:res.code}
http.request('api/wxlogin',data).then(ret=>{
wx.setStorageSync('openid', ret.data.openid)
})
}
})
}
wx.getLocation({
success:(res)=>{
wx.setStorageSync('lat', res.latitude)
wx.setStorageSync('lon', res.longitude)
}
})
},
globalData: {
userInfo: null
}
})
When using wechat location Need to be in app.json Configure the relevant configurations
"plugins": {
"calendar": {
"version": "1.1.0",
"provider": "wx92c68dae5a8bb046" // Calendar APPID
},
"chooseLocation": {
"version": "1.0.6",
"provider": "wx76a9a06e5b4e693e" // The plug-in APPid
},
"routePlan": {
"version": "1.0.12",
"provider": "wx50b5593e81dd937a"
}
},
"permission": {
"scope.userLocation": {
"desc": " Your location information will be used for applet location "
}
}2. Interact with the background to access the surrounding geographic information
Map related operations ( Include surrounding addresses as well as Relevant route planning )
wxml The code is as follows ( Example ):
<view class="page-body">
<view class="page-section page-section-gap">
<map
id="myMap"
style="width: 100%; height: 300px;"
latitude="{
{latitude}}"
longitude="{
{longitude}}"
markers="{
{marks}}"
covers="{
{covers}}"
show-location
></map>
</view>
<view wx:for="{
{address}}" wx:key='key' class="address">
<text style="float:left;">{
{item.site_name}}</text>
<text style="float:right;color:red;" bindtap="online" data-name="{
{item.site_address}}" data-lat="{
{item.lat}}" data-lon="{
{item.lon}}"> Plan the route </text>
</view>
</view>wxss The code is as follows :
.address{
width: 80%;
padding-left: 10%;
font-size: 30rpx;
height: 50rpx;
margin-top: 20rpx;
}JavaScript The code is as follows :
// pages/map/map.js
import Http from '../../utils/http';
let http=new Http()
const chooseLocation = requirePlugin('chooseLocation')
Page({
/**
* Initial data of the page
*/
data: {
latitude: wx.getStorageSync('lat'),
longitude: wx.getStorageSync('lon'),
address:[],
marks:[]
},
/**
* Life cycle function -- Monitor page loading
*/
onLoad: function (options) {
var that=this
http.request('api/site',{lat:that.data.latitude,lon:that.data.longitude},'Post').then(ret=>{
that.setData({address:ret.data}) // Call the interface to get the surrounding geographical location
// Set punctuation
var marks=[]
for(var i in ret.data){
marks.push({
'latitude':ret.data[i].lat,
'longitude':ret.data[i].lon,
'title':ret.data[i].site_name
})
}
that.setData({marks})
})
this.mapCtx = wx.createMapContext('mapId')
},
// Plan the route
online(e){
var data=e.currentTarget.dataset
let plugin = requirePlugin('routePlan');
let key = ''; // Using the application in Tencent location service key
let referer = ''; // Calling the plug-in app The name of
let endPoint = JSON.stringify({ // End
'name': data.name,
'latitude': data.lat,
'longitude': data.lon
});
wx.navigateTo({
url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
});
},
onshow(){
const location = chooseLocation.getLocation(); // If you click the confirm selection button , Then the selected point result object is returned , Otherwise return to null
if(location !=null){
this.setData({
latitude: location.latitude,
longitude:location.longitude,
address:location.name
})
}
},
/**
* Life cycle function -- Listening page first rendering completed
*/
onReady: function () {
},
/**
* Life cycle function -- Monitor page display
*/
onShow: function () {
},
/**
* Life cycle function -- Monitor page hidden
*/
onHide: function () {
},
/**
* Life cycle function -- Monitor page uninstall
*/
onUnload: function () {
},
/**
* Page related event handler -- Monitor user pull-down action
*/
onPullDownRefresh: function () {
},
/**
* Handling function of page pull bottom event
*/
onReachBottom: function () {
},
/**
* Users click the upper right corner to share
*/
onShareAppMessage: function () {
}
})summary
Use Tencent location and you're done
边栏推荐
- "As a service", the inevitable choice of enterprise digital transformation
- “即服务”,企业数字化转型的必然选择
- Small exercise of process and signal
- Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
- Defi "where does the money come from"? A problem that most people don't understand
- 深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
- 优思学院:六西格玛不只是统计!
- Optimization of unit test efficiency: why test programs? What are the benefits of testing?
- Loss function: Diou loss handwriting implementation
- What network security problems are exposed when a large-scale QQ number theft event occurs?
猜你喜欢

Loss function: Diou loss handwriting implementation

步骤详解 | 助您轻松提交 Google Play 数据安全表单

Waving flags and shouting for basic language

单元测试效率优化:为什么要对程序进行测试?测试有什么好处?

编程实战赛来啦!B站周边、高级会员等好礼送你啦!

Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集

Intelligent operation and maintenance: visual management system based on BIM Technology

Pytorch查看模型参数量和计算量

【刷题篇】避免洪水泛滥

Shell programming overview
随机推荐
JMeter transaction controller
Click the TD cell of table to open the dialog pop-up window. After obtaining the value, put the value back into the TD cell
[Title brushing] coco, who likes bananas
exlipse同时操作多行。比如同时在多行同列输入相同的文字
Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
[Title brushing] avoid flooding
Dart extended feature
深入理解.Net中的线程同步之构造模式(二)内核模式3.内核模式构造物Mutex
数据恢复软件EasyRecovery15下载
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
go time. after
What network security problems are exposed when a large-scale QQ number theft event occurs?
Chapter 13 signal (III) - example demonstration
Golang template (text/template)
notepad正则删除关键词所在行
Rpm2rpm packaging steps
Getting started with shell Basics
Impersonate server and client using message queuing
Jetpack Compose 实现完美屏幕适配
I love network security for new recruitment assessment