Use JSSDK to obtain geographical location from web pages
Copy a copy of the JSSDK environment and create an index.html file. The structure is as shown in Figure 7.1.
Figure 7.1 Section 7.1 File Structure
In location.js, encapsulate the "getLocation" interface as follows:
01 wxJSSDK.location = function(locationApi){ 02 if(wxJSSDK.isReady){ //wxJSSDK.isReady 查看微信JSSDK是否初始化完毕 03 if(locationApi){ 04 locationApi.getLocation && wx.getLocation({ //获取地理位置接口 05 success: function (res) { 06 locationApi.getLocation.success && 07 locationApi.getLocation.success(res); 08 } 09 }); 10 }else{ 11 console.log("缺少配置参数"); 12 } 13 }else{ 14 console.log("抱歉,wx没有初始化完毕,请等待wx初始化完毕,再调用位置接口服 15 务。"); 16 } 17 }
In the index.html file, add the "Get geographical location" button and display the location information after acquisition. The code structure is as follows:
01 <!DOCTYPE html> 02 <html lang="en"> 03 <head> 04 <metacharset="UTF-8"> 05 <meta name="viewport" content="width=device-width,initial-scale=1.0, 06 minimum-scale=1, maximum-scale=1.0,user-scalable=no"> 07 <title>第7章 7.1节 位置操作接口</title> 08 <!--依赖文件:jQuery--> 09 <script src="./js/jquery-1.11.2.min.js?1.1.10"></script> 10 <!--依赖文件:微信的JSSDK源文件--> 11 <scriptsrc="http://res.wx.qq.com/open/js/jweixin-1.0.0.js?1.1.10"></script> 12 <!--依赖文件:coolie--> 13 <script src="./js/cookie.js?1.1.10"></script> 14 <!--JSSDK的环境--> 15 <script src="./js/wxJSSDK.js?1.1.10"></script> 16 <!--引入检测API的位置服务--> 17 <script src="location.js?1.1.10"></script> 18 <style> 19 input{ 20 width: 100%; 21 padding: 0.2em; 22 background-color: #5eb95e; 23 font-size: 1.4em; 24 background-image: linear-gradient(to bottom, #62c462, #57a957); 25 background-repeat: repeat-x; 26 color: #ffffff; 27 text-align: center; 28 text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); 29 border-radius: 0.3em; 30 } 31 #info{ 32 border-left: 3px solid#03a9f4; 33 background-color: #5eb95e; 34 color: #ffffff; 35 border-radius: 0.3em; 36 text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); 37 } 38 </style> 39 </head> 40 <body> 41 <h1 style="font-size: 8em">:)</h1> 42 <b style="font-size: 3em">位置操作接口!</b><br/><br /> 43 <div id="info"> 44 <p>纬度:<em id="latitude">无</em></p> 45 <p>经度:<em id="longitude">无</em></p> 46 <p>速度:<em id="speed">无</em></p> 47 <p>位置精度:<em id="accuracy">无</em></p> 48 </div> 49 <input type="button" value="获取当前地理位置信息" id="getLocation" /> 50 </body> 51 </html>
Then add the response event in location.js, the code is as follows:
01 window.onload = function(){ 02 var latitude,longitude, speed ,accuracy; // 位置信息初始变量 03 $("#getLocation").click(function(){ // 获取地理位置接口 04 wxJSSDK.location({ 05 getLocation:{ 06 success:function (res) { 07 latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90 08 $("#latitude").html(latitude); 09 longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。 10 $("#longitude").html(longitude); 11 speed = res.speed; // 速度,以米/每秒计 12 $("#speed").html(speed); 13 accuracy = res.accuracy; // 位置精度 14 $("#accuracy").html(accuracy); 15 } 16 } 17 }); 18 }); 19 }
Finally, remember to add the "getLocation" API permissions to the JSSDK configuration environment.
[Code Explanation]
After the location of "getLocation" is successful, the relevant information of "Latitude", "Longitude", "Speed" and "Position Accuracy" will be returned. After clicking the "Get Location" button, WeChat will pop up a prompt message, as shown in Figure 7.2. The result after successfully obtaining the location service is shown in Figure 7.3.
Figure 7.2 WeChat JSSDK prompt for obtaining location information
Figure 7.3 Information about successful location service acquisition
"Practical Combat of WeChat Public Platform Web Development - HTML5+JSSDK Hybrid Development Decryption"
Welcome to exchange this book together
The above is the detailed content of Use JSSDK to obtain geographical location from web pages. 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

AI Hentai Generator
Generate AI Hentai for free.

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



How to send web pages to the desktop as a shortcut in Edge browser? Many of our users want to display frequently used web pages on the desktop as shortcuts for the convenience of directly opening access pages, but they don’t know how to do it. In response to this problem, the editor of this issue will share the solution with the majority of users. , let’s take a look at the content shared in today’s software tutorial. The shortcut method of sending web pages to the desktop in Edge browser: 1. Open the software and click the "..." button on the page. 2. Select "Install this site as an application" in "Application" from the drop-down menu option. 3. Finally, click it in the pop-up window

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

With the advancement of communication technology, telephone contact has become more and more common in our daily lives. However, sometimes we may try to call a number only to find that the number is no longer reachable. In order to save time and energy, many people start looking for empty number verification services to confirm the validity of the number. This service can help users quickly check whether a number is valid and avoid unnecessary trouble. 1. What are the empty account detection platforms? Users can check the status of the number, including whether it is empty, by dialing the corresponding inquiry number or visiting the official website through the official inquiry service provided by the operator. This service is provided by many operators to facilitate users to quickly understand the usage of the number. Online empty number detection platforms are now emerging in an endless stream on the market. These platforms provide quick and convenient

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Executing PHP code in a web page requires ensuring that the web server supports PHP and is properly configured. PHP can be opened in three ways: * **Server environment:** Place the PHP file in the server root directory and access it through the browser. * **Integrated Development Environment: **Place PHP files in the specified web root directory and access them through the browser. * **Remote Server:** Access PHP files hosted on a remote server via the URL address provided by the server.

PHP Practice: Code Example to Quickly Implement the Fibonacci Sequence The Fibonacci Sequence is a very interesting and common sequence in mathematics. It is defined as follows: the first and second numbers are 0 and 1, and from the third Starting with numbers, each number is the sum of the previous two numbers. The first few numbers in the Fibonacci sequence are 0,1,1.2,3,5,8,13,21,...and so on. In PHP, we can generate the Fibonacci sequence through recursion and iteration. Below we will show these two
