A single location

The API has a serious bug and the trigger condition is unknown. The small program may trigger after being placed in the background or locked on the screen. The result is that the obtained coordinates are stuck at a certain point after repeated calls, or there is a large jump and the coordinate moves back and forth between a fixed point and the current position. The bug appears on iOS and Android platforms, and the bug can appear across small programs, that is, after small program A is triggered, the same API will be called in small program B to trigger the bug, which can be solved by restarting the wechat program. It is estimated to be A problem of wechat. Therefore, the API is suitable for a single low frequency call.

wx.getLocation({
	type:'gcj02'.// Use gb coordinates
  isHighAccuracy: true.// High precision positioning, will call GPS to obtain high precision coordinates
  success: (res) = > {} // Locate the successful callback function
})
Copy the code

Continuous positioning

Continuous positioning requires multiple apis to work together

  • Wx.onlocationchange (function callback) registers the callback function triggered when the location is updated
  • Wx.offlocationchange (function callback) unlogs the callback function
  • Wx.startlocationupdate (Object Object) starts listening for position changes
  • Wx.stoplocationupdate (Object Object) Stops listening

This set of apis was tested bug-free and satisfied the requirement for continuous location, even when wx.getLocation was no longer working properly. But frequent calls to startLocationUpdate and stopLocationUpdate also seem to cause location data exceptions, so the order of calls needs to be followed:

  1. wx.startLocationUpdateOpen to monitor
  2. wx.onLocationChangeRegister listener functions
  3. Use this command when listening is not needed temporarilywx.offLocationChangeLog out of the listener function and use it when it is no longer neededwx.stopLocationUpdate.

The related documents

Developers.weixin.qq.com/miniprogram…