Geolocate User Location in R Leaflet

2,290 views
Skip to first unread message

Kyle Doherty

unread,
Mar 1, 2016, 12:25:31 PM3/1/16
to Shiny - Web Framework for R
I would like to use Shiny leaflet apps for field work, and it would be helpful to display the users location using the gps from their smart phone. 

It seems this is already possible in Leaflet: https://github.com/domoritz/leaflet-locatecontrol

I guess the greater question is, how can one install plugins for R Leaflet in a Shiny environment? 

Kyle Doherty

unread,
Mar 1, 2016, 4:53:01 PM3/1/16
to Shiny - Web Framework for R
I have had success with:

...but it does not dynamically update the position of the user, it only reports their initial position. Is there a way to modify the above so that it will update the users position every few seconds?

Huidong TIAN

unread,
Mar 2, 2016, 3:46:03 AM3/2/16
to Shiny - Web Framework for R
Do you mean let shiny-app read GPS automatically? if so, I guess it's not easy.

But if you mean text GPS data into shiny-app and display them, it's easy. 

This blog (shiny-app) may helpful to you. 



It use package "rCharts", which contains "leaflet". 

Kyle Doherty

unread,
Mar 2, 2016, 12:16:12 PM3/2/16
to Shiny - Web Framework for R
Yes, I want the app to read the users location using data from their smart phone gps, and update their position dynamically as you see in Google Maps app, for example. 

Joe Cheng

unread,
Mar 2, 2016, 2:24:50 PM3/2/16
to Kyle Doherty, Shiny - Web Framework for R
The JavaScript API is this:

https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition

So you could include a .js file (using includeScript(), for example) with the following:

navigator.geolocation.watchPosition(function(pos) {
  Shiny.onInputChange("userPosition", {
    latitude: pos.coords.latitude,
    longitude: pos.coords.longitude,
    altitude: pos.coords.altitude,
    accuracy: pos.coords.accuracy // in meters
    altitudeAccuracy: pos.coords.altitudeAccuracy // in meters,
    heading: pos.coords.heading,
    speed: pos.coords.speed
  });
}, null, {enableHighAccuracy: true});

That would let you observe input$userPosition from the server, and it should update whenever the position changes. You can then use leafletProxy to recenter the map on the new location, and/or move a marker, or whatever.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/16b76475-ac39-46c0-abc9-375ba336d836%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kyle Doherty

unread,
Mar 2, 2016, 2:53:28 PM3/2/16
to Shiny - Web Framework for R, kyledavi...@gmail.com
Unfortunately, I know nothing of Javascript, and so do not know how to create a js file. Is it simply a text document with your above script?

Secondly, how would you access the individual components of input$userPosition. Something like input$userPosition[1] for latitude?

Joe Cheng

unread,
Mar 2, 2016, 3:10:17 PM3/2/16
to Kyle Doherty, Shiny - Web Framework for R
Yes, create a file called "watchpos.js" right next to your ui.R file with that code pasted in, and somewhere in your ui.R file call includeScript("watchpos.js"). (I haven't tested the code so let me know if it doesn't work)

You can access those individual elements by name, like a named list. input$userPosition$latitude, for example. Be sure to check if input$userPosition is NULL; an easy way to do this is with req. http://shiny.rstudio.com/articles/req.html

Kyle Doherty

unread,
Mar 2, 2016, 9:13:37 PM3/2/16
to Shiny - Web Framework for R, kyledavi...@gmail.com
I have not had luck with the js file approach described above.

I am getting closer, however, by adapting the scripts from https://github.com/AugustT/shiny_geolocation

Please see this showcased example: https://bowkerlab.shinyapps.io/geotest/

It appears that the initial coordinates and accuracy is reported, but not dynamically updated, the location services continues to be active on an iDevice as long as the app is running.

I suspect it is an issue with my leafletProxy code, though I need an expert to verify. Perhaps it is not clearing the marker correctly? 

Kyle Doherty

unread,
Mar 3, 2016, 3:38:04 PM3/3/16
to Shiny - Web Framework for R
Thanks to Tom August and Karolis Kazlauskis for updating their scripts, their dynamically updating geolocation functionality is now documented at their github account here: 

Huidong TIAN

unread,
Mar 8, 2016, 4:05:19 AM3/8/16
to Shiny - Web Framework for R
Great thread! I don't know web browser can get access to GPS module also. Interesting, and I will also make a similar shiny-app
Reply all
Reply to author
Forward
0 new messages