Created
May 14, 2020 17:06
-
-
Save tieniber/aafebadcd2f14f2e181267ef45b21128 to your computer and use it in GitHub Desktop.
A Mendix JS action that creates and returns a new object containing your user's current location.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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