Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Mendix JS action that creates and returns a new object containing your user's current location.
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import { Big } from "big.js";
// BEGIN EXTRA CODE
async function createMxObj(entity) {
return new Promise(function(resolve,reject) {
mx.data.create({
entity: entity,
callback: function(obj) {
resolve(obj);
},
error: function(e) {
reject("Could not create object:", e);
}
}
);
});
}
async function getCurrentLocation() {
return new Promise(function(resolve,reject) {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(resolve, reject, options);
});
}
// END EXTRA CODE
/**
* @returns {Promise.<MxObject>}
*/
export async function JS_GetCurrentLocation() {
// BEGIN USER CODE
const pos = await getCurrentLocation();
const crd = pos.coords;
const obj = await createMxObj("MyFirstModule.Location");
obj.set("latitude", crd.latitude);
obj.set("longitude", crd.longitude);
obj.set("accuracy", crd.accuracy);
return obj;
// END USER CODE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment