search
HomeWeb Front-endH5 TutorialMini program to learn how to obtain geolocation and display city names

What this article brings to you is how the mini program learns how to obtain geographical positioning and display city names, so that everyone can understand how to display the corresponding city name when positioning in the WeChat mini program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I was looking at the WeChat applet recently and encountered a problem with geolocation displaying city names. This article is to record the process of solving this problem.

Solution

The wx.getLocation() of the applet obtains the longitude and latitude and does not include the place name, so you need to use the longitude and latitude The corresponding map converts the place name (this article uses Baidu Map).

Process

1. Code:

1), code details:

wx.getLocation(object): Get the current geographical location and speed.

Note: User authorization for geolocation permission is required.

For detailed parameter description, please view the mini program API: https://developers.weixin.qq.com/miniprogram/dev/api/location.html#wxgetlocationobject

Let’s take a look at wx. The data obtained after the success of getLocation():

From the obtained data, we can see that there is no place name we want, so we need to convert the longitude and latitude into For the corresponding place names, this article uses the corresponding function of Baidu Map to convert the corresponding place names.

2), Preparation work for Baidu Map

① Before using Baidu Map API, you must first obtain the key ak of Baidu Map , ak is generated by Baidu Maps party;

②. Open Baidu Maps open platform, select "Development Documents" > "WeChat Mini Program JavaScript API" in the navigation bar, and click on "Getting Started Guide" "There is a detailed introduction on how to generate the key ak, which will not be introduced in this article.

Reference document: http://lbsyun.baidu.com/index.php?title=wxjsapi/guide/key

③. Copy the generated ak and paste the ak into the mini program middle. In fact, Baidu Maps provides a compressed package of the map API used by the mini program, but due to size restrictions when publishing the mini program, I chose to use links.

Baidu map online conversion link address: https://api.map.baidu.com/geocoder/v2/?ak=obtained AK&location=' latitude ',' longitude '&output=json'

④. Use the mini program account to log in to the WeChat public platform (https://mp.weixin.qq.com/), select "Development Settings" in "Settings", and add the URL of the Baidu Map API to the request in a legal domain name.

Note: Baidu Map provides a download package for the Mini Program API. If you don’t want to use the link address, you can use the download package. Due to the size limit when the Mini Program is released, this article uses links. The address does not use the download package.

Let us take a look at the parameter output in success:


There must be one of the parameters obtained Suitable for you~~~

2. Attached is the complete code:

##Data binding in wxml.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of Mini program to learn how to obtain geolocation and display city names. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

HTML5 Features: The Core of H5HTML5 Features: The Core of H5May 04, 2025 am 12:05 AM

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

H5: Exploring the Latest Version of HTMLH5: Exploring the Latest Version of HTMLMay 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Beyond Basics: Advanced Techniques in H5 CodeBeyond Basics: Advanced Techniques in H5 CodeMay 02, 2025 am 12:03 AM

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools