screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useEffect } = React; const App = () => { const [latitude, setLatitude] = useState('35.929673'); const [longitude, setLongitude] = useState('-78.948237'); const [address, setAddress] = useState(''); const handleGetLocation = () => { if (latitude && longitude) { // Convert latitude and longitude to numbers const lat = parseFloat(latitude); const lon = parseFloat(longitude); if (!isNaN(lat) && !isNaN(lon)) { setLatitude(lat); setLongitude(lon); } } }; useEffect(() => { if (latitude !== '' && longitude !== '') { const nominatimApiUrl = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}`; fetch(nominatimApiUrl) .then(response => response.json()) .then(data => { if (data.address) { console.log(data.address); const { city, state, country, postcode } = data.address; setAddress(` ${city}, ${state}, ${country},${postcode}`); } else { setAddress('Address not found'); } }) .catch(error => { console.log(error); setAddress('An error occurred while fetching the address'); }); } }, [latitude, longitude]); return ( <div className="container"> <h3>React Js get Address Latitude and Longitude</h3> Latitude: <input type="text" value={latitude} onChange={e => setLatitude(e.target.value)} /><br /> Longitude: <input type="text" value={longitude} onChange={e => setLongitude(e.target.value)} /><br /> <button onClick={handleGetLocation}>Get Location</button> <br /> Address: {address} </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); width: 600px; margin: 0 auto; padding: 20px; display: flex; flex-direction: column; align-items: center; justify-content: center; } h3 { font-size: 24px; margin-bottom: 15px; } input { width: 100%; padding: 10px; margin: 5px 0; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; } button { padding: 10px 20px; background-color: #007bff; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } br { margin: 10px 0; } .address { font-size: 18px; margin-top: 15px; } </style> </body> </html>