In this blog, we will look at how we can get the current location of the device with the click of a button.
Implementation
Syntax:
Xrm.Device.getCurrentPosition().then(successCallback, errorCallback)
Code:
var formCustomizations = {
getLocation: function (executionContext) {
var formContext = executionContext;
Xrm.Device.getCurrentPosition().then(
function success(location) {
formContext.getAttribute("vv_latitude").setValue(location.coords.latitude);
formContext.getAttribute("vv_longitude").setValue(location.coords.longitude);
},
function (error) {
Xrm.Navigation.openAlertDialog({ text: error.message });
}
);
}
}
- Call the “getLocation” function on button.


- After doing everything successfully when you will click on a button you will get latitude and longitude.


NOTE:
- This method is supported only for mobile clients.
- Location setting must be enabled in Dynamics 365 for Phones app
Follow below steps to achieve this:
- Go to settings

- click on enable location

- click on OK to give permission

- Dynamics 365 for Phones app must have the permission of Location in your mobile device.
