How to get weather data from OpenWeatherMap API in a popup by clicking any location on the map?
i am still new at calling APIs in my map, API return JSON files but i don't know how to call the API in my map and how to bind JSON file in an HTML element.
This is the API which uses coordinates to return weather data in a JSON file: api.openweathermap.org/data/2.5/weather?lat=35&lon=139
but i need to replace these lat and lng parameters by clicking any location on the map.
Best Answer
You should be able to do something like this
The example is using the
fetch
api to call the openweathermap api andfetch
is only supported in modern browsers. If you need to support older browsers you need to use a polyfill (https://github.com/github/fetch)Edit: As mentioned by @Spacedman in the comments, the above solution should only be used on private networks. If you use the above code on public available networks, you expose your apikey for openweathermap so that anyone can use it to request the service.