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 id="">:)</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!

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
