Get User Location

Get Current Location of the User

(): Promise
Description: Call this method to get the current location of the user. You will need to first ask for permission before accessing this method. Refer Ask for Permissions.

Usage

// First ask for user location permission
import { Constants } from 'phonepesdk'

let result = await sdk.seekPermission([Constants.Permission.READ_SMS, Constants.Permission.LOCATION])

//Then ask for location
sdk.getCurrentLocation()
.then((location) => { 
  console.log("Location received with latitude = " + location.latitude + ' longitude = ' + location.longitude)  
})
.catch((err) => { 
  console.log("Error found when fetching location = " + err) 
})
Promise resolve:
{
  'latitude': 123.123, 
  'longitude': 345.345
}

Promise reject:
{
  'error_code': 'LOCATION_FETCH_FAILURE'
}

Response Parameters

Promise<any>
Promise resolve is called with user's current location.
Otherwise, Promise reject is called with the appropriate error_code.
The promise rejection reason can be as follows:

Code

Description

LOCATION_FETCH_FAILURE

Unable to fetch user's location

Listening to PhonePe's User Location Updates (Special Usage)

1. Register for location update callbacks

(callbackName:

Description: Call this method to register your callback for location updates

Request Parameters

Parameter Name

Type

Description

callbackName

string

A unique name for your callback, that will help in uniquely identifying the callback

response

LocationResponseCallback = (response: any): void

A callback to register for user's location updates

Usage:

sdk.registerLocationUpdateSuccessCallback('callbackName', (response) => {
  console.log('Location success response = ' + JSON.stringify(response)) 
})

 //Response
 {
    'latitude': 123.123, 
    'longitude': 345.345
 }

2. (Optional) Register for failure in updating location

(callbackName:

Description: Call this method to register your callback for location update FAILURES

Usage:

sdk.registerLocationUpdateFailureCallback('callbackName', (response) => {
   console.log('Location failure response = ' + JSON.stringify(response)) 
 })
 
//Response
 {
   'error_code': 'LOCATION_UPDATE_FAILURE'
 }

3. Call `startUpdatingLocation` API to start receiving callbacks to the registered through `registerLocationUpdateSuccessCallback` above.

Description: This method is used get continuous location updates of the user. This is used in conjunction with @see registerLocationUpdateSuccessCallback to get notified when a user changes location. You SHOULD call stopUpdatingLocation when done as described in the next section. This helps preserve battery. For fetching location just once getCurrentLocation as stated above in the section "Get Current Location of the User"

Usage:

sdk.startUpdatingLocation()

Stop listening to PhonePe's User Location Updates (Special Usage)

Description: This method is used stop listening to location updates of the user. You should call this method if you don't need to continuously know about the user location. It's very very IMPORTANT to call this method when you would want to stop listening to the location updates. This preserves the user's battery and overall enhances the user experience.

Usage:

sdk.stopUpdatingLocation()