📝 Improve getLocation set variable snippet (#1213)
Added location error handling. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Updated location retrieval functionality: - Renamed function for clarity. - Enhanced location information to include latitude and longitude. - Improved error handling for smoother user experience. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Baptiste Arnaud <baptiste.arnaud95@gmail.com>
This commit is contained in:
@ -127,17 +127,24 @@ This value block allows you to find the `Id` from `Ids` with the same index as `
|
||||
For this you can provide the following custom code:
|
||||
|
||||
```js
|
||||
function getPosition() {
|
||||
return new Promise((resolve, reject) => {
|
||||
function getLocation() {
|
||||
return new Promise((resolve) => {
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(position) => resolve(position),
|
||||
(error) => reject(error),
|
||||
position => resolve(`${position.coords.latitude}, ${position.coords.longitude}`),
|
||||
error => resolve("error"),
|
||||
{ enableHighAccuracy: true, timeout: 5000 }
|
||||
)
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const coords = (await getPosition()).coords
|
||||
const coords = await getLocation();
|
||||
|
||||
return `${coords.latitude}, ${coords.longitude}`
|
||||
// Check for error
|
||||
if (coords === "error") {
|
||||
return "Unable to get location";
|
||||
}
|
||||
|
||||
return coords;
|
||||
```
|
||||
|
||||
This custom function can only work when it is executed on the client browser so you need to make sure to enable the "Execute on client" option.
|
||||
|
Reference in New Issue
Block a user