Mini 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.
There must be one of the parameters obtained Suitable for you~~~
##Data binding in wxml.
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!

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.