Question

I'm trying to use rmaps http://rmaps.github.io/ to draw the route a vehicle follows onto a map. In my ideal world, the map should: * be interactive (i.e. zoomable, scrollable) * store and display metadata (e.g. get a popup when you click on a marker) * show the actual path travelled by the vehicle (either using straight lines between waypoints, or possibly even following the road). * be embeddable in a Shiny webpage

It's very easy to add markers to a Leaflet map via rMaps. I was just wondering whether it is possible to add other structures such as are available in Leaflet.js, such as Polyline and Polygon. If not, is such functionality planned for a future release?

I've looked through the rMaps code for leaflet here: https://github.com/ramnathv/rMaps/blob/master/R/Leaflet.R and I don't see anything that I recognise. There's a function for geoJson which I'm not sure how to use.

Was it helpful?

Solution 2

In Order to add a poligon (such as a circle) you should:

  1. Go to the location of the html page of the actual map (you should see the location in the browser) and open it, with right click, preferable with notepad ++
  2. look for :

    var map = L.map(spec.dom, { fullscreenControl: true }).setView(spec.center, spec.zoom);

    if (spec.provider){
      L.tileLayer.provider(spec.provider).addTo(map)    
    } else {
    L.tileLayer(spec.urlTemplate, spec.layerOpts).addTo(map)
    }
    

and right after that you can add a nice circle with the following code, (dont forget to save the file):

L.circle([29.7601927,-95.3693896],7200,{color:'red'}).addTo(map).bindPopup('some thing').addTo(map)

for more info, just see the leaflet API in http://leafletjs.com/reference.html

btw, rmaps is just an interface for R and leaflet, if you want to create real crazy stuff, you should get used to open the html file and add java script code from the leaflet API, people created amazing plugins, much more that just adding polygons, for cool examples see:

http://leafletjs.com/plugins.html

i also really like this one:

http://threatwiki.thesentinelproject.org/iranvisualization

Just to add some additional info of how rMaps works(that's also true for rCharts)

  1. All that rMaps does, is to create the javascript code, the build of the map is done by your web browsr, which means that if you will go to the library that rmaps is installed and copy it to another computer without R, the html file will run perfectly, so that means you can create a rMap, and then add any other elements from the leaflet API that are not supported by rMaps, manually, in the html file, just as i explained (i showed how to do it for "circle").

  2. For shiny apps, you cant add thing manually, so you'll have to make sure that the elements from the API are supported in rmaps. in order to know which elements of the API are supported, first create a map:

    map <- Leaflet$new()

and then look in all of the elements of "map", and compare it to the leaflet API. for instance the marker creation command that you can find in the API:

http://leafletjs.com/reference.html#marker (from the leaflet API), which is created by:

L.marker([51.5, -0.09]).bindPopup('Hi. I am a popup').addTo(map);

has an equivalent in rMaps:

map$marker(
  c(51.5, -0.09),
  bindPopup = 'Hi. I am a popup'
)

Just go step by step for each element in leaflet API you are interested in, if it doesn't exist, then you can ask ramnath in Github to add it https://github.com/ramnathv/rMaps/pulls , or create by your own a function that does that in the R code.

OTHER TIPS

I have been looking at the sort of the same problem. There is a "leaflet-routing-machine" plugin that you can find here (https://github.com/perliedman/leaflet-routing-machine) which has functions to compute the routes between waypoints. Their demo is pretty cool!

I have been trying to plot the route between two locations using rMaps and this leaflet plugin. I am still working on having my code work (I guess I am missing something), but here is how it looks so far.

library(rMaps)
library(leaflet)

map = Leaflet$new()
map$setView(c(50.6067, 3.6254), 6)
map$tileLayer(provider="MapQuestOpen.OSM")
map$marker(c(48.8588,2.3469), bindPopup = 'Pickup Location')
map$marker(c(52.3546,4.9039), bindPopup = 'Dropoff Location')
map

map$addAssets(jshead = c("leaflet-routing-machine.min.js"))
map$setTemplate(afterScript = sprintf("
   <script>
   L.Routing.control({
       waypoints: [
           L.latLng(48.8588,2.3469),
           L.latLng(52.3546,4.9039)
       ],
       geocoder: L.Control.Geocoder.nominatim(),
       plan: L.Routing.plan(null, {
           waypointIcon: function(i) {
                return new L.Icon.Label.Default({ labelText: String.fromCharCode(65 + i)});
           }
       })
   }).addTo(map);
   </script>
 "))
map

At least, I thought it might be helpful if you take a look at this plugin, though I haven't completely figured out how to get the route plotted yet!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top