getCurrentPosition (Client API reference)

Returns the current location using the device geolocation capability.

Available for

This method is supported only for the mobile clients.

Syntax

Xrm.Device.getCurrentPosition().then(successCallback, errorCallback)

Parameters

Parameter Name Type Required Description
successCallback Function Yes A function to call when the current geolocation information is returned. A geolocation object with the following values is passed to the function.:
- coords: Contains a set of geographic coordinates along with associated accuracy and a set of other optional values such as altitude and speed.
- timestamp: Represents the time when the object was acquired and is represented as DOMTimeStamp.
errorCallback Function Yes A function to call when the operation fails. An object with the following properties is passed:
- code: The error code. Number.
- message: Localized message describing the error details. String.

If the user location setting isn't enabled on your mobile device, the error message indicates the same. If you're using an earlier version of the model-driven apps mobile client or if geolocation capability isn't available on your mobile device, null is passed to the error callback.

Return Value

On success, returns a geolocation object with the values specified earlier in the successCallback function.

Exceptions

See Web service error codes

Remarks

For the getCurrentPosition method to work, the geolocation capability must be enabled on your mobile device, and the model-driven apps mobile clients must have permissions to access the device location, which isn't enabled by default.

Example

Xrm.Device.getCurrentPosition().then(
    function success(location) {
        Xrm.Navigation.openAlertDialog({
            text: "Latitude: " + location.coords.latitude +
            ", Longitude: " + location.coords.longitude
        });
    },
    function (error) {
        Xrm.Navigation.openAlertDialog({ text: error.message });
    }
);

Xrm.Device