Skip to content

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。

    PropertiesTypeDefault valueRequiredDescription
    typestringwgs84Nowgs84 returns gps coordinates, which can be used for wx.openLocation
    altitudestringfalseNoPassing true will return altitude information. Since obtaining altitude requires high accuracy, it will slow down the interface return speed
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    latitudenumberLatitude, range is -90~90, negative number indicates south latitude
    longitudenumberLongitude, range is -180~180, negative number indicates west longitude
    speednumberSpeed, unit m/s
    accuracynumberPosition accuracy
    altitudenumberHeight, unit m
    verticalAccuracynumberVertical accuracy, unit m (cannot be obtained on Android, return 0)
    horizontalAccuracynumberHorizontal accuracy, unit m
  • Sample code:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    typenumberWhen selecting a city, the value is 1, and when selecting a precise location, the value is 4
    citynumberCity name
    namestringLocation name
    addressstringDetailed address
    latitudenumberLatitude, floating point number, range is -90~90, negative number indicates south latitude. (To be deprecated)
    longitudenumberLongitude, 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。

    PropertiesTypeDefault valueRequiredDescription
    typestringtencentNoTencent Map tencent, Google Map google, only IDE supports
    latitudenumber-NoDestination Latitude
    longitudenumber-NoDestination Longitude
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    namestringLocation name
    addressstringDetailed address
    latitudenumberLatitude, floating point number, range is -90~90, negative number indicates south latitude.
    longitudenumberLongitude, 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。
    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback 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。
    PropertiesTypeDefault valueRequiredDescription
    typestringgcj02Nowgs84 returns gps coordinates, which can be used for wx.openLocation
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback 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。
    PropertiesTypeDefault valueRequiredDescription
    typestringgcj02Nowgs84 returns gps coordinates, which can be used for wx.openLocation
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback 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。

    PropertiesTypeDefault valueRequiredDescription
    typestringtencentNoTencent Map tencent, Google Map google, only supported by IDE
    latitudenumber-YesLatitude, range is -90~90, negative numbers indicate south latitude.
    longitudenumber-YesLongitude, range is -180~180, negative numbers indicate west longitude.
    scalenumber18NoZoom ratio, range 5~20
    namestring-NoLocation name
    addressstring-NoDetailed description of the address
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
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:

    PropertiesTypeDescription
    errCodenumberError 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:

    PropertiesTypeDescription
    latitudenumberLatitude, range is -90~90, negative number indicates south latitude
    longitudenumberLongitude, range is -180~180, negative number indicates west longitude
    speednumberSpeed, unit m/s
    accuracynumberPosition accuracy
    altitudenumberHeight, unit m
    verticalAccuracynumberVertical accuracy, unit m (cannot be obtained on Android, return 0)
    horizontalAccuracynumberHorizontal accuracy, unit m
  • Sample code:

js
 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:

js
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:

js
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。

    PropertiesTypeDefault valueRequiredDescription
    typestringwgs84Nowgs84 returns gps coordinates, which can be used for wx.openLocation
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    latitudenumberLatitude, range is -90~90, negative number indicates south latitude
    longitudenumberLongitude, range is -180~180, negative number indicates west longitude
  • Sample code:

js
wx.getFuzzyLocation({
 type: 'wgs84',
 success (res) {
   const latitude = res.latitude
   const longitude = res.longitude
 }
})