In JavaScript you can get the coordinates of a location using Nominatim's API (open-source geocoding API).
Example:
javascript// In this example, we will assume that top level await is implemented // First, let's define the address; it can be a text from an input let address = 'Gran Via de les Corts Catalanes 162' // Let's replace the spaces by '+' signs to perform the query let query = address.split(' ').join('+') // Gets the response in JSON format let response = await fetch(`https://nominatim.openstreetmap.org/search.php?q=${query}&format=jsonv2`) // At this point we'll have an array of n suggestions let suggestions = await response.json() // In this example, you take the first coordinates let lat = suggestions[0]?.lat let lon = suggestions[0]?.lon console.log(lat, lon) // 41.3667655 2.1385636
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.