Did you know you can create Model-Driven apps to run on mobile devices with offline capabilities? This functionality recently went GA . You now have the ability to operate Model-Driven Apps using Power Apps mobile on your mobile device. When your app is launched through Power Apps mobile, you can access certain device capabilities, like it location, through JavaScript.
The Xrm.Device JavaScript library provides methods that allow you to access your devices native capabilities. Here is a sample JavaScript function that uses the Xrm.Device.getCurrentPosition function to get the devices latitude and longitude and updates the fields on the form.
function getLocation(executionContext) {
var formContext = executionContext.getFormContext();
Xrm.Device.getCurrentPosition().then(
function success(location) {
formContext.getAttribute("jg_latitude").setValue(location.coords.latitude);
formContext.getAttribute("jg_longitude").setValue(location.coords.longitude);
},
function (error) {
alert({ text: error.message });
}
);
}
I can use this in a form and if I run this on a phone then the latitude and longitude files are populated using the phone’s location.
If try and run this app in the browser you will get an error as the getCurrentPosition is not supported in browsers. If you plan to run your Model-Driven app on different devices then use the getClient function to determine the type of device you app is running on.
function getLocation(executionContext) {
var formContext = executionContext.getFormContext();
var clientContext = Xrm.Utility.getGlobalContext().client;
var client = clientContext.getClient();
if (client == "Mobile")
{
Xrm.Device.getCurrentPosition().then(
function success(location) {
formContext.getAttribute("jg_latitude").setValue(location.coords.latitude);
formContext.getAttribute("jg_longitude").setValue(location.coords.longitude);
},
function (error) {
alert({ text: error.message });
}
);
}
}