my.getLocation

Get the current geographical location of the user.

Sample Code

copy
my.getLocation({
      success(res) {
        my.hideLoading();
        console.log(res)
        that.setData({
          hasLocation: true,
          location: formatLocation(res.longitude, res.latitude)
        })
      },
      fail() {
        my.hideLoading();
        my.alert({ title: 'location failed' });
      },
    })

Parameters

NameTypeMandatoryDescriptionLowest version
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-
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:

NameTypeDescriptionLowest version
longitudeStringLongitude-
latitudeStringLatitude-
accuracyStringAccuracy, in m-

Error Code

errorDescriptionSolution
11Make sure the location related right   has been enabledPrompt the user to enable location   right
12Network abnormity, try again laterPrompt the user to check the current   network
13Location failure, try again later-
14Service location timeoutPrompt the user to try again