当前位置:网站首页>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
边栏推荐
- go time. after
- 【Kubernetes系列】K8s设置MySQL8大小写不敏感
- Yousi College: Six Sigma is not just statistics!
- Dart extended feature
- Tencent two sides: @bean and @component are used on the same class. What happens?
- This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc
- MySQL access denied, opened as Administrator
- notepad正则删除关键词所在行
- Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
- get请求与post提交区别的简易理解
猜你喜欢

单元测试效率优化:为什么要对程序进行测试?测试有什么好处?
![[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)](/img/5a/eaa845f4332f0b8ee8b6409d6a79e8.png)
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)

Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)

QQ was stolen? The reason is

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

半导体动态杂谈

"As a service", the inevitable choice of enterprise digital transformation

用Unity实现Flat Shading

Problems in QT creator (additional unknown and error lines are listed in the debug output window)

Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set
随机推荐
PHP reverses scenarios based on code and skillfully uses debug_ backtrace()
Optimization of unit test efficiency: why test programs? What are the benefits of testing?
知识传播不能取代专业学习!
Three uses of golang underscores
Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)
Calculates the length of the last word in a string, separated by spaces
Crypto questions
Flat shading with unity
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
Apache Doris comparison optimization Encyclopedia
This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc
Prometheus 2.29.0 new features
科普达人丨漫画图解什么是eRDMA?
Knowledge dissemination cannot replace professional learning!
Jetpack compose for perfect screen fit
Race of golang
Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
单元测试效率优化:为什么要对程序进行测试?测试有什么好处?
【刷题篇】供暖器
Wuenda 2022 machine learning special course evaluation is coming!