Location
getLocation
TIP
The API usage is as follows: wx.getLocation(Object object)
TIP
- If you use this interface, you need to declare it in app.json, otherwise you will not be able to use it normally.
- You need to 'user authorization' scope.userLocation before calling.
Functional description: Get the current geographic location and speed. This interface cannot be called after the user leaves the applet. High-frequency calls will cause power consumption. If necessary, you can use the continuous positioning interface wx.onLocationChange
Parameters and descriptions: Object object。
Properties Type Default value Required Description type string wgs84 No wgs84 returns gps coordinates, which can be used for wx.openLocation altitude string false No Passing true will return altitude information. Since obtaining altitude requires high accuracy, it will slow down the interface return speed success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description latitude number Latitude, range is -90~90, negative number indicates south latitude longitude number Longitude, range is -180~180, negative number indicates west longitude speed number Speed, unit m/s accuracy number Position accuracy altitude number Height, unit m verticalAccuracy number Vertical accuracy, unit m (cannot be obtained on Android, return 0) horizontalAccuracy number Horizontal accuracy, unit m Sample code:
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
}
})TIP
- Position simulation in the tool uses IP positioning, which may have a certain error. And the tool currently only supports gcj02 coordinates.
- When using a third-party service for reverse address resolution, please confirm the default coordinate system of the third-party service and perform coordinate conversion correctly.
choosePoi
TIP
The API usage is as follows: wx.choosePoi(Object object)
TIP
- If this interface is used, scope.userLocation needs to be declared in app.json, otherwise the interface will not be used normally.
Functional description: Open the POI list to select a location, supporting fuzzy positioning (accurate to the city) and precise positioning mixed selection.
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description type number When selecting a city, the value is 1, and when selecting a precise location, the value is 6 city number City name name string Location name address string Detailed address latitude number Latitude, floating point number, range is -90~90, negative number indicates south latitude. (To be deprecated) longitude number Longitude, floating point number, range is -180~180, negative number indicates west longitude. (To be deprecated)
chooseLocation
TIP
The API usage is as follows: wx.chooseLocation(Object object)
TIP
- If this interface is used, scope.userLocation needs to be declared in app.json, otherwise the interface will not be used normally.
Functional description: Open the map to select the location
Parameters and descriptions: Object object。
Properties Type Default value Required Description type string tencent No Tencent Map tencent, Google Map google, only IDE supports latitude number - No Destination Latitude longitude number - No Destination Longitude success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description name string Location name address string Detailed address latitude number Latitude, floating point number, range is -90~90, negative number indicates south latitude. longitude number Longitude, floating point number, range is -180~180, negative number indicates west longitude.
stopLocationUpdate
TIP
The API usage is as follows: wx.stopLocationUpdate(Object object)
- Functional description: Turn off monitoring real-time location changes, and stop receiving messages in both the front and back ends.
- Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure)
startLocationUpdateBackground
TIP
The API usage is as follows: wx.startLocationUpdateBackground(Object object)
- Functional description: Open the mini program to receive location messages when entering the front and back ends, and guide the user to turn on 'authorization'. After authorization, the mini program can accept location message changes while running or entering the back end.
- Parameters and descriptions: Object object。
Properties Type Default value Required Description type string gcj02 No wgs84 returns gps coordinates, which can be used for wx.openLocation success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure)
startLocationUpdate
TIP
The API usage is as follows: wx.startLocationUpdate(Object object)
TIP
- If you use this interface, you need to declare scope.userLocation in app.json, otherwise you will not be able to use this interface normally.
- Functional description: Open the applet to receive location messages when entering the foreground.
- Parameters and descriptions: Object object。
Properties Type Default value Required Description type string gcj02 No wgs84 returns gps coordinates, which can be used for wx.openLocation success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure)
openLocation
TIP
The API usage is as follows: wx.openLocation(Object object)
Functional description: Use built-in maps to view locations
Parameters and descriptions: Object object。
Properties Type Default value Required Description type string tencent No Tencent Map tencent, Google Map google, only supported by IDE latitude number - Yes Latitude, range is -90~90, negative numbers indicate south latitude. longitude number - Yes Longitude, range is -180~180, negative numbers indicate west longitude. scale number 18 No Zoom ratio, range 5~22 name string - No Location name address string - No Detailed description of the address success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.getLocation({
type: 'gcj02', //wx.openLocation
success (res) {
const latitude = res.latitude
const longitude = res.longitude
wx.openLocation({
latitude,
longitude,
scale: 18
})
}
})onLocationChangeError
TIP
The API usage is as follows: wx.onLocationChangeError(function listener)
Functional description: Triggered when the continuous positioning interface fails to return.
Parameters and descriptions: function callback callback function, parameter Object res is as follows:
Properties Type Description errCode number Error code
onLocationChange
TIP
The API usage is as follows: wx.onLocationChange(function listener)
Functional description: To listen to real-time geographic location change events, you need to combine wx.startLocationUpdateBackground,wx.startLocationUpdate When using
Parameters and descriptions: function callback,The listener function of the real-time geographic location change event, the parameter Object res is as follows:
Properties Type Description latitude number Latitude, range is -90~90, negative number indicates south latitude longitude number Longitude, range is -180~180, negative number indicates west longitude speed number Speed, unit m/s accuracy number Position accuracy altitude number Height, unit m verticalAccuracy number Vertical accuracy, unit m (cannot be obtained on Android, return 0) horizontalAccuracy number Horizontal accuracy, unit m Sample code:
const _locationChangeFn = function(res) {
console.log('location change', res)
}
wx.onLocationChange(_locationChangeFn)
wx.offLocationChange(_locationChangeFn)offLocationChangeError
TIP
The API usage is as follows: wx.offLocationChangeError(function listener)
Functional description: Remove the listener function triggered when the continuous positioning interface fails to return.
Parameters and descriptions: function listener, onLocationChangeError The listener function passed in. If this parameter is not passed, all listeners are removed.
Sample code:
const listener = function (res) { console.log(res) }
wx.onLocationChangeError(listener)
wx.offLocationChangeError(listener) // Pass the same function object used for listening.offLocationChange
TIP
The API usage is as follows: wx.offLocationChange(function listener)
Functional description: Remove the listener function of the real-time geographic location change event.
Parameters and descriptions: function listener, onLocationChange listener function passed in. If this parameter is not passed, all listener functions will be removed.
Sample code:
const listener = function (res) { console.log(res) }
wx.onLocationChange(listener)
wx.offLocationChange(listener) // Pass the same function object used for listening.getFuzzyLocation
TIP
The API usage is as follows: wx.getFuzzyLocation(Object object)
Functional description: Get the current fuzzy location
Parameters and descriptions: Object object。
Properties Type Default value Required Description type string wgs84 No wgs84 returns gps coordinates, which can be used for wx.openLocation success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description latitude number Latitude, range is -90~90, negative number indicates south latitude longitude number Longitude, range is -180~180, negative number indicates west longitude Sample code:
wx.getFuzzyLocation({
type: 'wgs84',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
}
})