* whtie label revamp * minor changes * updated beta info in copilot docs * renamed gds to ds in overview doc of v2.15 onwards * updated multienv: renamed gds to ds, updated screenshot * updated multienv: app state description * updated superadmin wrt new licensing updates * licensing update: free trial * updates in licensing * changes after review * [docs]updated restapi with bearer auth * Update kubernetes-aks.md * Update openshift.md * Update ecs.md * Update ecs.md * Update kubernetes-gke.md * Update kubernetes.md * Update docker.md * Update docker.md * Update ecs.md * Update google-cloud-run.md * Update kubernetes-aks.md * Update kubernetes-gke.md * Update kubernetes.md * Update openshift.md * Update kubernetes-gke.md * Update kubernetes-aks.md * fixed ecs, removed heroku from 2.15 onwards * updated digitalocean doc * licensing doc changes and location * Update digitalocean.md * Update digitalocean.md * added v2.18.0 --------- Co-authored-by: Adish M <44204658+adishM98@users.noreply.github.com>
3.2 KiB
| id | title |
|---|---|
| access-users-location | Access a user's location |
Access a user's location using RunJS query (Geolocation API)
In this how-to guide, we will build a ToolJet application that will utilize the JavaScript Geolocation API to get the user's location. The Geolocation API provides access to geographical location data associated with a user's device. This can be determined using GPS, WIFI, IP Geolocation and so on.
:::info To protect the user's privacy, Geolocation API requests permission to locate the device. If the user grants permission, you will gain access to location data such as latitude, longitude, altitude, and speed. :::
-
Let's start by creating a new application
-
In the app editor, go to the query panel at the bottom and create a RunJS query by selecting Run JavaScript Code as the datasource
-
You can use the following javascript code that makes use of geolocation api to get the location
function getCoordinates() { return new Promise(function(resolve, reject) { navigator.geolocation.getCurrentPosition(resolve, reject); }); } async function getAddress() { // notice, no then(), cause await would block and // wait for the resolved result const position = await getCoordinates(); let latitude = position.coords.latitude; let longitude = position.coords.longitude; return [latitude, longitude]; } return await getAddress() -
Now, go to the Advanced tab and enable the
Run query on page load?option. Enabling this option will run this javascript query every time the app is opened by the user and the query will return the location -
Save the query and hit the fire button
-
As soon as you hit the fire button, the browser will prompt you to allow the permission to share the location access to ToolJet app. You'll need to allow it to return the location data
-
Now, to check the data returned by the query go to the Inspector on the left sidebar. Expand the queries ->
runjs1(query name) -> and then expand the data. You'll find the coordinates
-
Next, we can use these coordinates returned by the query on the map component to show the location. Drop a map component on the canvas and edit its properties. In the Initial location property, enter
{{ {"lat": queries.runjs1.data[0], "lng": queries.runjs1.data[1]} }}
-
Finally, you'll see the location updated on the map component