ramnathv / rMaps

Interactive Maps from R
http://rmaps.github.io
389 stars 194 forks source link

add objects with functions to geoJSON #14

Open HarlanH opened 10 years ago

HarlanH commented 10 years ago

I've successfully figured out how to plot points and stuff with the geoJSON functionality and leaflet, but I don't see how to add things like functions to the objects. I tried to create something like this (from the Leaflet docs):

{
    onEachFeature: function (feature, layer) {
        layer.bindPopup(feature.properties.description);
    }
}

but instead got this:

 var geojsonLayer = L.geoJson(spec.features 
        , 
         [
 "{ onEachFeature: function (feature, layer) {
        layer.bindPopup(feature.properties.description);
    }
}" 
]
      ).addTo(map)

Obviously that doesn't work. If that's not yet supported, can you specify how you'd like this to work, and I'll see if I can figure out how to implement it and create a pull request...

ramnathv commented 10 years ago

@HarlanH A good example of using the geoJSON functionality in Leaflet is seen in the bikeshare application. I have a detailed slide deck on how this application was built, which you can find here.

In short, the Leaflet binding in rCharts, which is what I have moved to rMaps currently supports adding raw javascript objects directly by enclosing them within '#!' and '!#' tags. Here is the relevant code from my slides that shows how the onEachFeature callback can be added to display popups.

L1$geoJson(toGeoJSON(bike), 
  onEachFeature = '#! function(feature, layer){
    layer.bindPopup(feature.properties.popup)
  } !#'
)

I am still experimenting with the best way to send javascript objects directly from R in a secure manner. So, the exact mechanism might change, but the feature is an important one and will be supported.

HarlanH commented 10 years ago

Ahah! That is extremely helpful! And I was able to hack together some circles using the geoJSON interface too.

ramnathv commented 10 years ago

Yes. In the example I linked to, I use another JS function to style the markers as circular points. Let me know if you are missing anything else. As I said, real use cases motivate development :)