问题
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