openweathermap weather tile in Leaflet in R

南楼画角 提交于 2019-12-10 11:42:33

问题


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.


回答1:


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.




回答2:


I am using the addProviderTiles function instead of raw javascript or the openweather library. For this I had to add the apiKey within providerTileOptions for my OpenWeatherMap account:

mw = leaflet() %>%
    addProviderTiles(providers$CartoDB.Positron) %>%
    setView(-122.36075812146, 35.6759920119894, zoom = 11) %>% 
    addProviderTiles(providers$OpenWeatherMap.Wind,  
        options=providerTileOptions(apiKey="<myAPIkey>"))              

mw


来源:https://stackoverflow.com/questions/53137105/openweathermap-weather-tile-in-leaflet-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!