'How to convert city name to coordinates in JS?

Is there a way to convert city name into coordinates?

Openweather api provides access to historical data but i need to have "lat" and "lon" coordinates instead of just city name like in others of their endpoints.

I have an input which i just simply submit when city is typed in and then store it in state. State is then provided to url like this:

`https://api.openweathermap.org/data/2.5/forecast?q=${location}&appid=&units=metric`

Unfortunately it does not work for historical data. Here i need lat and lon coordinates.

`https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=52.237049&lon=21.017532&dt=1652436000&appid=&units=metric`


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source