Implementation of WeChat Mini Program City Positioning
This article carefully introduces to you the implementation example of city positioning in the WeChat mini program. It mainly realizes the acquisition of relevant information about the city information of the current country. The introduction in the article is very detailed. I believe it has certain reference and learning value for everyone. It is necessary Friends, let’s take a look below.
Preface
In the WeChat applet, we can get the current location of the device by calling wx.getLocation()
The geographical location information, this information is the longitude and latitude of the current location. If we want to get information such as which country and city our current location is in, how can we achieve this?
Implementation method
The WeChat applet does not provide such an API, but it doesn’t matter, there is wx.getLocation()
The obtained longitude and latitude are enough as the basis. For others, we can use other third-party map services to implement, such as Tencent Map or Baidu Map API.
Taking Tencent Map as an example, we can register an account on the Tencent Map Open Platform and then create a key in its management background.
Then in the top menu, you can find the WebServiceAPI menu:
Tencent Map WebServiceAPI
Tencent Map provides a lot of WebServiceAPI, such as according to The address obtains the longitude and latitude, and finds the address based on the longitude and latitude. What we are going to use is to find the address based on the longitude and latitude, which is also called "reverse address resolution":
Reverse address resolution
Reverse geocoding provides conversion from coordinates to text descriptions of coordinate locations. The calling form is an API in the form of an HTTP URL. The basic usage is as follows:
http://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
The basic parameter of this URL is a longitude and latitude coordinate address. . You can replace the key in this URL with your own key and view it directly in the browser to see results similar to this. You can also get richer information by passing in different parameter options:
{ "status": 0, "message": "query ok", "request_id": "6225548022856589453", "result": { "location": { "lat": 39.984154, "lng": 116.30749 }, "address": "北京市海淀区北四环西路66号彩和坊路", "formatted_addresses": { "recommend": "海淀区中关村彩和坊路中国技术交易大厦", "rough": "海淀区中关村彩和坊路中国技术交易大厦" }, "address_component": { "nation": "中国", "province": "北京市", "city": "北京市", "district": "海淀区", "street": "彩和坊路", "street_number": "北四环西路66号" }, "ad_info": { "adcode": "110108", "name": "中国,北京市,北京市,海淀区", "location": { "lat": 39.984154, "lng": 116.307487 }, "nation": "中国", "province": "北京市", "city": "北京市", "district": "海淀区" }, "address_reference": { "business_area": { "title": "中关村", "location": { "lat": 39.984058, "lng": 116.307518 }, "_distance": 0, "_dir_desc": "内" }, "famous_area": { "title": "中关村", "location": { "lat": 39.984058, "lng": 116.307518 }, "_distance": 0, "_dir_desc": "内" }, "crossroad": { "title": "彩和坊路/北四环西路辅路(路口)", "location": { "lat": 39.985001, "lng": 116.308113 }, "_distance": 104.2, "_dir_desc": "西南" }, "village": { "title": "稻香园北社区", "location": { "lat": 39.983269, "lng": 116.301979 }, "_distance": 480.1, "_dir_desc": "东" }, "town": { "title": "海淀街道", "location": { "lat": 39.984154, "lng": 116.307487 }, "_distance": 0, "_dir_desc": "内" }, "street_number": { "title": "北四环西路66号", "location": { "lat": 39.984119, "lng": 116.307503 }, "_distance": 6.9, "_dir_desc": "" }, "street": { "title": "彩和坊路", "location": { "lat": 39.984154, "lng": 116.308098 }, "_distance": 49.1, "_dir_desc": "西" }, "landmark_l1": { "title": "北京中关村创业大街", "location": { "lat": 39.984055, "lng": 116.306992 }, "_distance": 43.9, "_dir_desc": "东" }, "landmark_l2": { "title": "中国技术交易大厦", "location": { "lat": 39.984154, "lng": 116.307487 }, "_distance": 0, "_dir_desc": "内" } } } }
From the return result of this API, we can see that it contains the address information we want, such as country, city, district, etc.
Next, we need to call this API in our code. The API can be called through JSONP, or it can be called on the server side. I called it in my own server. The following is my code, implemented using Node.js Express, for reference only:
// 服务调用地址:http://localhost:3000/lbs/location router.get('/lbs/location', function (req, res, next) { let lat = req.query.latitude let lng = req.query.longitude request.get({ uri: 'https://apis.map.qq.com/ws/geocoder/v1/', json: true, qs: { location: `${lat},${lng}`, key: '你的腾讯地图密钥key' } }, (err, response, data) => { if (response.statusCode === 200) { responseUtil.jsonSuccess(res, data) } else { responseUtil.jsonError(res, 10001, '') } }) })
Then, you can take a look at the Page code on the mini program:
Page({ data: { address: {} }, onLoad: function () { //获取当前经纬度信息 wx.getLocation({ success: ({latitude, longitude}) => { //调用后台API,获取地址信息 wx.request({ url: 'http://localhost:3000/lbs/location', data: { latitude: latitude, longitude: longitude }, success: (res) => { let info = res.data.data.result.ad_info this.setData({ address: info }) }, fail: () => { }, complete: () => { } }) } }) } })
And a simple small program interface for displaying these address information:
<view> <view>{{address.nation}}</view> <view>{{address.city}}</view> <view>{{address.district}}</view> </view>
The running results are as follows:
Run Result
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to obtain the latitude and longitude coordinates of your location through the WeChat applet
##WeChat How to use the three-level linkage selector of the mini program
The above is the detailed content of Implementation of WeChat Mini Program City Positioning. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











With the popularity of mobile Internet technology and smartphones, WeChat has become an indispensable application in people's lives. WeChat mini programs allow people to directly use mini programs to solve some simple needs without downloading and installing applications. This article will introduce how to use Python to develop WeChat applet. 1. Preparation Before using Python to develop WeChat applet, you need to install the relevant Python library. It is recommended to use the two libraries wxpy and itchat here. wxpy is a WeChat machine

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

Mini programs can use react. How to use it: 1. Implement a renderer based on "react-reconciler" and generate a DSL; 2. Create a mini program component to parse and render DSL; 3. Install npm and execute the developer Build npm in the tool; 4. Introduce the package into your own page, and then use the API to complete the development.

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

Implementation idea: Establishing the server side of thread, so as to process the various functions of the chat room. The establishment of the x02 client is much simpler than the server. The function of the client is only to send and receive messages, and to enter specific characters according to specific rules. To achieve the use of different functions, therefore, on the client side, you only need to use two threads, one is dedicated to receiving messages, and the other is dedicated to sending messages. As for why not use one, that is because, only

This article brings you some related issues about WeChat mini programs. It mainly introduces how to use official account template messages in mini programs. Let’s take a look at them together. I hope it will be helpful to everyone.

Geolocation positioning and map display of PHP and mini programs Geolocation positioning and map display have become one of the necessary functions in modern technology. With the popularity of mobile devices, people's demand for positioning and map display is also increasing. During the development process, PHP and applets are two common technology choices. This article will introduce you to the implementation method of geographical location positioning and map display in PHP and mini programs, and attach corresponding code examples. 1. Geolocation in PHP In PHP, we can use third-party geolocation
