Analysis of WeChat applet location API interface
This article mainly introduces the relevant information of the WeChat applet location API interface. Here is a detailed introduction to the location API interface and a simple example code. Friends in need can refer to
WeChat applet location API interface:
Now that WeChat mini programs are very popular, I used my vacation time to learn the basics of WeChat mini programs, hehe!
The following is a record of learning the WeChat applet location API interface, and a small example was written to record it. Please correct me if there are any errors.
WeChat applet has two location interfaces:
1. wx.getLocation(OBJECT) to obtain the current geographical location and speed.
2. wx.openLocation(OBJECT) Use WeChat’s built-in map to view the location
Then, according to the object parameter description, combined with module modularization, the next two interfaces are rewritten and exposed. References make project management more flexible. The specific code is as follows:
location.js::
/** * 获取当前的地理位置、速度。 * 1、fType: 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于wx.openLocation的坐标 选填 * 2、cbSuccessFun: 接口调用成功的回调函数,返回内容详见返回参数说明。 必填 * 3、cbFailFun: 接口调用失败的回调函数 选填 * 4、cbCompleteFun:接口调用结束的回调函数(调用成功、失败都会执行) 选填 */ function getLocationFun(fType, cbSuccessFun, cbFailFun, cbCompleteFun){ var getObj={}; getObj.type="wgs84"; if(fType){ getObj.type=fType; } getObj.success=function(res){ var _res=res; if(cbSuccessFun){ cbSuccessFun(_res); } } getObj.fail=function(res){ if(cbFailFun){ cbFailFun(); }else{ console.log("getLocation fail:"+res.errMsg); } } getObj.complete=function(res){ if(cbCompleteFun){ cbCompleteFun(); } } wx.getLocation(getObj); } /** * 使用微信内置地图查看位置 * 1、latitude: 纬度,范围为-90~90,负数表示南纬 必填 * 2、longitude: 经度,范围为-180~180,负数表示西经 必填 * 3、scale: 缩放比例,范围1~28,默认为28 选填 * 4、name: 位置名 选填 * 5、address: 地址的详细说明 选填 * 6、cbSuccessFun: 接口调用成功的回调函数 选填 * 7、cbFailFun: 接口调用失败的回调函数 选填 * 8、cbCompleteFun:接口调用结束的回调函数(调用成功、失败都会执行) 选填 */ function openLocationFun(latitude, longitude, scale, name, address, cbSuccessFun, cbFailFun, cbCompleteFun){ var openObj={}; openObj.latitude=latitude; openObj.longitude=longitude; openObj.scale=15; if(scale>0 && scale<29){ openObj.scale=scale; } if(name){ openObj.name=name; } if(address){ openObj.address=address; } openObj.success=function(res){ if(cbSuccessFun){ cbSuccessFun(); } } openObj.fail=function(res){ if(cbFailFun){ cbFailFun(); }else{ console.log("openLocation fail:"+res.errMsg); } } openObj.complete=function(res){ if(cbCompleteFun){ cbCompleteFun(); } } wx.openLocation(openObj); } module.exports={ getLocationFun: getLocationFun, openLocationFun: openLocationFun }
demo.js::
var comm = require( "../../common/common.js" ); var location=require('../../common/location.js'); Page( { data: { uploadImgUrls: [], title: "" }, getlocation: function( e ) { location.getLocationFun( 'gcj02', function(cb){ console.log(cb); var _latitude=cb.latitude; var _longitude=cb.longitude; location.openLocationFun( _latitude, _longitude, null, "厦门观音山", "厦门观音山匹克大厦", null, null, null ) } ) }, onLoad: function( options ) { var _title = "ddd"; if( options.title ) { _title = options.title; } this.setData( { title: _title }) console.log("load") console.log( comm.formatDateFun( new Date(), 1 ) ); }, onShow:function(e){ console.log("show"); }, onHide: function(e){ console.log("hide"); }, onUnload:function(e){ console.log("unload"); } // onReady: function(){ // wx.setNavigationBarTitle({ // title: this.data.title // }); // } })
After debugging, it was found that the type of the getLocation interface, whether it is passed wgs84 or gcj02, returns only the latitude and longitude, and does not have the speed and position accuracy mentioned in the document. Parameter
# Then after I clicked the "Go here" page to jump, I found that the positioning failed every time. I don't know if it is because of the web development tool. reason. And there seems to be a gap in longitude and latitude, which is inconsistent with my actual distance. I also defined two parameters, name and address, but no changes were found. The final, more serious problem is that after I clicked Return, it prompted a page route error. When I clicked the button again, it prompted an error and could not be clicked. I do not know why? How to solve it!
# This is the point where I have learned about this interface. I will update if there are other discoveries or solutions in the future.
============================================== ==================================================
Today, WeChat released a new version [the latest version 0.10.101100], and there are further updates to the location interface.
1. When opening the map interface, the page route error will not be prompted when returning
2. After passing the custom name and address parameters through the wx.openLocation interface, they can be displayed in the map description box, but the longitude and latitude are still not accurate enough. Clicking "Go here" still fails to position.
#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:
Analysis of Video API in WeChat Mini Program
About the selector (time) of WeChat Mini Program , date, region) analysis
The WeChat applet displays json data to the applet through the API interface
The above is the detailed content of Analysis of WeChat applet location API interface. 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

Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

An API interface is a specification for interaction between software components and is used to implement communication and data exchange between different applications or systems. The API interface acts as a "translator", converting the developer's instructions into computer language so that the applications can work together. Its advantages include convenient data sharing, simplified development, improved performance, enhanced security, improved productivity and interoperability.

Free api interface website: 1. UomgAPI: a platform that provides stable and fast free API services, with over 100 API interfaces; 2. free-api: provides multiple free API interfaces; 3. JSON API: provides free data API interface; 4. AutoNavi Open Platform: Provides map-related API interfaces; 5. Face recognition Face++: Provides face recognition-related API interfaces; 6. Speed data: Provides over a hundred free API interfaces, suitable for various needs In the case of data sources; 7. Aggregate data, etc.

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

API interface types are rich and diverse, including RESTful API, SOAP API, GraphQL API, etc. RESTful API communicates through the HTTP protocol, with a simple and efficient design, which is the current mainstream Web API design style. SOAP API is based on XML, focuses on cross-language and platform interoperability, and is mostly used in large enterprises and government agencies. GraphQL API is a new query language and runtime environment that supports flexible data query and response.

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3.
