Get Device Location

Retrieves the current geographical coordinates of the user's device.

Description

The getDeviceLocation() action fetches the latitude and longitude of the user. This is used to provide personalized content based on the user's current position, such as identifying the closest retail branch or providing localized currency/shipping estimates.

Function Signature

VajroSDK.actions.getDeviceLocation()

Structure of Response

StatusResponse
Success{ "status": "success", "message": "Action executed successfully" }
Cart not accessible{ "status": "error", "errorId": 400, "message": "Action not executed"}
Unexpected Error{ "status": "error", "errorId": 500, "errorHandle": "unknown-error", "message": "Something unexpected happened" }

Example Usage

Best Practices

  1. Check Permission State First: Always verify the current permission status using VajroSDK.variables.device.permissions.location. If it's denied, show a custom UI with instructions on how to enable it in Settings rather than calling the action and getting an error.
  2. Use Lazy Loading: Only call getDeviceLocation() when the user performs a specific action that requires it, such as clicking a "Use Current Location" button. Avoid calling it automatically on every page load to save battery and respect privacy.

Caveats

  1. Accuracy Variance: The precision of the coordinates (latitude/longitude) depends on whether the device is using GPS, Wi-Fi, or cellular towers. Indoor locations may return a significantly larger "accuracy radius."
  2. User Permission: Unlike some web APIs, this requires the app-level permission to be granted. If the user selects "Ask Next Time" or "Never," the action will redirect the user to get permission.