Web Geolocation API to Get User's Location

Hi,

Is there a way to use, or a plan to implement the web Geolocation API for asking the userā€™s location and continue processing based on that?

Thanks

1 Like

Hey @aghasemi ,

Yep, just use streamlit-bokeh-events with this bunch of code and it works pretty well :slight_smile:

PS. you can execute any custom js this way on a event and get the value in the callback without writting a full component :wink:

Hope it helps !

7 Likes

That really deserves more publicity!

And many thanks.

1 Like

Hello!
This code is working but the location I am getting is not my current location, but my wifi serviceā€™s.
Please help
Thankyou

1 Like

Thanks for providing this nice solution. I noticed that if the web app is embedded (ā€œiframeā€) in an HTML, the get location button is not responding. See the examples below.

Working: https://share.streamlit.io/giswqs/streamlit-geospatial/app.py?page=Create+Timelapse
Not working: https://streamlit.gishub.org
HMTL: streamlit-geospatial/index.html at gh-pages Ā· giswqs/streamlit-geospatial Ā· GitHub

1 Like

@ash2shukla , thanks for the idea.
Iā€™ve tried it out locally, it works well on my laptop but my mobile device isnā€™t reacting to the button click (I can see the page with the button, but clicking on it doesnā€™t seem to do anything).

Any ideas what Iā€™m doing wrong or what I need to do to get it to work on an Android device?

1 Like

For others who may pass by here, I wrote an alternative, maybe less verbose and more lightweight solution which can be used to not only ask for userā€™s location, but also call virtually any JavaScript/Web API from Streamlit and get the result. Here it is: GitHub - aghasemi/streamlit_js_eval: A custom Streamlit component to evaluate arbitrary Javascript expressions

Feedback is very much appreciated.

4 Likes

Hi! I wanted to express my gratitude for providing such a useful component. Itā€™s exactly what I was looking for. However, Iā€™d like to offer some feedback. In your example, itā€™s not very clear how to use the get_geolocation method. It seems to suggest that it returns the coordinates directly (latitude and longitude), but in reality, it returns JSON data. To retrieve the coordinates, one needs to navigate through the JSON structure, accessing ā€œcoordsā€ and then ā€œlatitudeā€ and ā€œlongitudeā€ fields. This discrepancy between the example and the actual method behavior could be clarified. Providing a more detailed example demonstrating the usage of this method would be greatly appreciated. Nonetheless, I find your component awesome! Thank you so much for creating it!

1 Like

Hi. Thanks a lot for your interest.

If you look at MDN documents for the GeoLocation API, that explains what data the API provides. I wanted to return all that information, not just the latitude/longitude (i.e. the coords object).

1 Like

thank you I used yours :heart: