accessing GPS #257
-
Hi there, Just came across nicegui and trying to get familiar with the project. I was wondering - is it possible to access the GPS capabilities of a device? e.g. is it possible to do browser geolocation and access the Navigator.geolocation object? You have an example of how to make custom vuejs components - is it possible to effectively make one to do this function? Could you, for example, use these utilities: https://vueuse.org Thanks so much |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Sure, you could certainly create a custom component for getting the geolocation. But if you basically just want to call a JavaScript function, you can also use async def get_location():
result = await ui.run_javascript('''
return await new Promise((resolve, reject) => {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
position => resolve({
lat: position.coords.latitude,
lon: position.coords.longitude,
}),
error => reject(error),
);
} else {
reject("Geolocation is not supported by this browser.");
}
});
''', timeout=10)
ui.notify(str(result))
ui.button('Get location', on_click=get_location) It is a bit tricky since you need to await the result, but the pattern Note the 10 second timeout: You might need to wait for the user to grant location access. This might take even longer. |
Beta Was this translation helpful? Give feedback.
-
Thanks so much. This is great. Looking forward to having a proper go building something with nicegui - I've always liked the Quasar project and to integrate in this way into the python ecosystem is exciting! |
Beta Was this translation helpful? Give feedback.
Sure, you could certainly create a custom component for getting the geolocation.
But if you basically just want to call a JavaScript function, you can also use
ui.run_javasscript
: