Search code examples
javascriptropenweathermaphtmlwidgetsr-leaflet

openweathermap weather tile in Leaflet in R


I am trying to add custom weather tiles on a leaflet map in a Shiny application using leaflet-openweathermap javascript library available here. I am not conversant with javascript and the map doesn't render the weather layers.

I first downloaded leaflet-openweathermap.js and placed it in www/js folder in my app's path. I then registered the plugin:

openWeatherPlugin <- htmlDependency(
  "Leaflet.OpenWeather",
  "1.6.0",
  src = normalizePath(path = getwd()),
  script = "www/js/leaflet.openweathermap.js"
)

To render a weather layer on leaflet, this is what I tried:

leaflet() %>% 
  addTiles() %>% 
  registerPlugin(openWeatherPlugin) %>% 
  onRender("
            function(el, x){
            L.OWM.clouds({showLegend: true, opacity: 0.5, appId: 'MY_APP_ID'})
            }
           ")

MY_APP_ID is a valid ID obtained from openweathermap.org However the above code doesn't generate the desired weather layer of clouds. I am not conversant with javascript and don't know what's wrong with this code. Appreciate some help please.


Solution

  • What if you add .addTo(this); in the onRender call, like:

      onRender("function(el, x){
              L.OWM.clouds({showLegend: true, opacity: 0.5, appId: 'MY_APP_ID'}).addTo(this);
            }
           ")
    

    and the javascript file is called leaflet-openweathermap.js and you've got leaflet.openweathermap.js or did you change to hyphen to a point?

    I am not getting any clouds with your API key. So I dont think the ID is valid, since I'm getting this msg in the console.

    [HTTP/1.1 401 Unauthorized 99ms]

    The appId is your private key, not the name.

    It works with a working key.

    enter image description here