my.getLocation

Get the current geographical location of the user.

Supported types of mini programs: DSL, H5+

Sample code

copy
my.getLocation({
  type: 1,
  success: (res) => {
    console.log('getLocation Success:', JSON.stringify(res, null, 2))
  },
  fail: (err) => {
    console.log('getLocation Failed:', JSON.stringify(err, null, 2))
  }
});

Parameters

PropertyType

Required

Description
cacheTimeoutNumberNolongitude and latitude location cache expiry time in seconds. Default is 30s. Use of cache can speed up location process. Re-location is done upon cache expiry.
typeNumberNo0: default, get the longitude and latitude.

coordinateSystemType

String

String

Retrieves the type of coordinate system used for positioning.

Valid values:

  • WGS84 (Default value): returns GPS coordinates.
  • GCJ02: returns GCJ coordinates, which can be used for mapping and positioning in mainland China.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).

Success Callback Function

The incoming parameter is of the Object type with the following attributes:

PropertyTypeDescription
longitudeStringLongitude.
latitudeStringLatitude.
accuracyStringAccuracy, in m.

Error code

Error

Error message

Solution

2

Invalid parameters

Refer to the previous Parameters table to ensure that all parameters are specified correctly, and that all required parameters are provided.

3

Unknown error

Please contact technical support for assistance.

11

Make sure the location related right   has been enabled.

Prompt the user to enable location permission.

13

Location failure, try again later.

Please try again later. If you still have problems, contact technical support for assistance.

14

Service location timeout. (Android only)

Prompt the user to try again.