Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 750 Bytes

File metadata and controls

22 lines (19 loc) · 750 Bytes

Get Current address using Javascript Code

We can use browser nagivigator to get current location. And using that location's latitude and longitude we invoke the openstreetmap api to get current address.

navigator.geolocation.getCurrentPosition((position) => {
  const { latitude, longitude } = position.coords;
  const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}`;
  fetch(url)
    .then((res) => res.json())
    .then((data) => {
      console.log(data.display_name);
      console.table(data.address);
    })
    .catch(() => console.error("Error on fetching data."));
});

DEMO

Click here to see the demo

https://my-address.netlify.app