当前位置:网站首页>Wechat applet - get user location (longitude and latitude + city)
Wechat applet - get user location (longitude and latitude + city)
2022-07-26 02:17:00 【Mocode】
Wechat applet - Get the user's location ( Longitude and latitude + city )
List of articles
One 、 The goal is
Get the user's city
Two 、 Realize the idea
1. Use the interface function of wechat applet to obtain the longitude and latitude of the user's position
2. Change latitude and longitude Inverse analysis For structured text addresses
3. Extract the required address structure components according to the structured text address , Such as province 、 City 、 Districts and counties, etc .
3、 ... and 、 Implementation steps
3.1 Interface functions used
Wechat applet - Get the interface function of the user location :wx.getLocation(Object object)
You also need to use applet configuration :permission
Tencent location service - Reverse address resolution ( Coordinate position description ) The interface function :reverseGeocoder(options:Object)
3.2 Specific steps
3.2.1 Create interface
wxml file
<view class="view1"> Click to get user location </view>
<view class="view2"> Longitude of the user's location :{
{latitude}}</view>
<view class="view2"> Latitude of the user's location :{
{longitude}}</view>
<view class="view2"> User City :{
{city}}</view>
wxss file
.view1 {
background-color: yellow;
width: 100%;
height: 200rpx;
margin-bottom: 20rpx;
}
.view2 {
background-color: yellow;
width: 100%;
height: 100rpx;
margin-bottom: 20rpx;
}
js file
// index.js
// Get application instance
const app = getApp()
Page({
data: {
latitude: null,
longitude: null,
city: null
},
onLoad() {
},
})
Interface effect 
3.2.2 Get the latitude and longitude of the user's position
Write a function that triggers the acquisition of longitude and latitude of the user's position getLocation, Call in function wx.getLocation Interface , Get the longitude 、 Latitude and other information , Then bind to the front-end interface .
3.2.2.1 stay app.json Configuration in file permission
When users first use this function , The applet asks the user whether he is authorized to obtain the user's location information . Don't ask again .( Clear the cache of developer tools 、 After recompiling the applet, it will ask again , Because the authorization information of the previous user has been cleared )
app.json file
{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin",
"navigationBarTextStyle":"black"
},
"style": "v2",
"sitemapLocation": "sitemap.json",
// Add the following code ( The above code is already available after creating the applet project )
"permission": {
"scope.userLocation": {
"desc": " Your location information will be used to show the effect of the applet location interface "
}
}
}
3.2.2.2 call wx.getLocation Interface
wxml file
// newly added bindtap This function , Make the user click this view It can trigger the function of obtaining the user's location
<view class="view1" bindtap="getLocation"> Click to get user location </view>
js file
// index.js
// Get application instance
const app = getApp()
Page({
data: {
latitude: null,
longitude: null,
city: null
},
onLoad() {
},
// Add the following code
getLocation() {
var that = this;
wx.getLocation({
type: 'wgs84',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
console.log(res) // Output the obtained longitude and latitude information to the console for inspection
that.setData({
// Will get the longitude 、 The latitude values are assigned to local variables
latitude: res.latitude,
longitude: res.longitude
})
}
})
}
})
effect


3.2.3 Get the user's city
3.2.3.1 Configuration outside the applet
Apply for one in Tencent location service key
according to Tencent location service - Wechat applet Javascript In the development guide 【 Introduction and use restrictions 】 The instructions in , Complete the following steps .

Download the applet JavaScriptSDK
stay Tencent location service website Download it ( A choice , I downloaded it v1.2), Save to wechat applet project directory .
Unzip the downloaded compressed package to the current folder .
Security domain name settings
Sign in Wechat applet public platform , Using this applet appid( Or the test number of your wechat account ) Sign in , stay “ Server domain name ” To configure request Legitimate domain name :https://apis.map.qq.com.

3.2.3.2 call reverseGeocoder Interface
js file
// index.js
// Get application instance
const app = getApp()
// newly added
// introduce SDK Core class , Write this according to the path you put SDK Location of core classes
var QQMapWX = require('../../qqmap-wx-jssdk.js');
// newly added
// Instantiation API Core class
var qqmapsdk = new QQMapWX({
key: '……' // Required , Fill in your application for Tencent location service key
});
Page({
data: {
latitude: null,
longitude: null,
city: null
},
onLoad() {
},
getLocation() {
var that = this;
wx.getLocation({
type: 'wgs84',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
console.log(res)
that.setData({
latitude: res.latitude,
longitude: res.longitude
})
// newly added
qqmapsdk.reverseGeocoder({
// coordinates , Get the current location by default , Not a required parameter
//Object Format
location: {
latitude: res.latitude,
longitude: res.longitude
},
success: function(res) {
// Callback after success
console.log(res.result.ad_info.city);
that.setData({
city: res.result.ad_info.city
})
},
fail: function(error) {
console.error(error);
},
complete: function(res) {
console.log(res);
}
})
}
})
}
})
effect 
Reference resources
边栏推荐
- Bitmap这个“内存刺客”你要小心~
- [red team] att & CK - using bits services to achieve persistence
- 记录之目标检测NMS(非极大值抑制)
- 2022-07-17
- 18_ Request file
- [2020] [paper notes] growth of bi2te3/cofeb double-layer heterojunction by magnetron sputtering——
- 1. Mx6ul core module serial Ethernet test (VII)
- Dqn pytoch example
- SQL manual blind injection and error reporting injection
- [cloud native] 4.1 Devops foundation and Practice
猜你喜欢

What are the functions of cloud notes, and how do browsers add cloud note plug-ins

I.MX6UL核心模块使用连载-RS485测试 (十)

I.MX6UL核心模块使用连载-触摸屏校准 (九)
![Web3.0 blog DAPP development practice [2022]](/img/18/f386246ff6ffbd0a42df57c3cd9170.png)
Web3.0 blog DAPP development practice [2022]

I.MX6UL核心模块使用连载-以太网测试 (七)

2. Login - verification code function and saving login status

Wechat applet decryption and unpacking to obtain source code tutorial

I.MX6UL核心模块使用连载-TF卡读写测试 (五)

Prometheus + process exporter + grafana monitor the resource usage of the process

DialogRPT-Dialog Ranking Pretrained Transformers
随机推荐
c# 单元测试
Ti AM335X工控模块使用beaglebone(bbb)的Debian系统
[C language brush leetcode] 443. Compressed string (m)
图解B+树的插入过程
What are the functions of cloud notes, and how do browsers add cloud note plug-ins
我来图书馆小程序签到流程分析
Primary key b+ tree, secondary index b+ tree and corresponding query process analysis
19_ Request forms and documents
增删改查业务的快速上手
Binary logs in MySQL
租户问题。
IDEA运行web项目出现乱码问题有效解决(附详细步骤)
国标GB28181协议视频平台EasyGBS消息弹框模式优化
Advantages of composition API
Are you still using ==0 null equal to judge null values? How much do you know about isempty and isblank
【PyQt5打包为exe】
Monitoring of debezium synchronization debezium
AttributeError: ‘Document‘ object has no attribute ‘pageCount‘
Postman报Json序列化错误
信息系统项目管理师---第十章沟通管理和干系人管理历年考题