google earth api elevation code example

Example: how to get elevation with google elevation api

const [currentLatitude, setCurrentLatitude] = useState(0);
const [currentLongitude, setCurrentLongitude] = useState(0);
const [currentElevation, setCurrentElevation] = useState(0);

  useEffect( async () => {
    try {
      const res = await fetch(`https://maps.googleapis.com/maps/api/elevation/json?locations=${currentLatitude},${currentLongitude}&key={your_API}`);
      const data = await res.json()
      const currentElevation = JSON.stringify(data.results[0].elevation);
      setCurrentElevation(currentElevation);
    } catch (error) {
      console.log(error)
    }
}, [currentLongitude]);

Tags:

Misc Example