Geolocation은 μ‚¬μš©μžμ˜ μœ„μΉ˜ 정보λ₯Ό 얻을 수 μžˆλŠ” μ›Ή 기술 쀑 ν•˜λ‚˜λ‘œ, HTML5μ—μ„œ μ§€μ›λ©λ‹ˆλ‹€. μ•„λž˜λŠ” HTML5 Geolocation을 μ‚¬μš©ν•˜λŠ” λͺ‡ κ°€μ§€ κ°„λ‹¨ν•œ μ˜ˆμ‹œμž…λ‹ˆλ‹€.

  1. ν˜„μž¬ μœ„μΉ˜ κ°€μ Έμ˜€κΈ°: μ‚¬μš©μžμ˜ ν˜„μž¬ μœ„μΉ˜λ₯Ό κ°€μ Έμ˜€λŠ” κ°€μž₯ 기본적인 μ˜ˆμ‹œμž…λ‹ˆλ‹€.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Geolocation Example</title> </head> <body> <button onclick="getLocation()">Get Location</button> <p id="demo"></p> <script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { document.getElementById("demo").innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { document.getElementById("demo").innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } </script> </body> </html>
  1. 지도에 ν˜„μž¬ μœ„μΉ˜ ν‘œμ‹œν•˜κΈ°: ν˜„μž¬ μœ„μΉ˜λ₯Ό Google Maps둜 ν‘œμ‹œν•˜λŠ” μ˜ˆμ‹œμž…λ‹ˆλ‹€.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Geolocation Map Example</title> <style> #map { height: 400px; width: 100%; } </style> </head> <body> <div id="map"></div> <script> function initMap() { var map = new google.maps.Map(document.getElementById('map'), { zoom: 12 }); if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = { lat: position.coords.latitude, lng: position.coords.longitude }; map.setCenter(pos); var marker = new google.maps.Marker({ position: pos, map: map, title: 'Your Location' }); }, function() { handleLocationError(true, map.getCenter()); }); } else { handleLocationError(false, map.getCenter()); } } function handleLocationError(browserHasGeolocation, pos) { var infoWindow = new google.maps.InfoWindow({map: map}); infoWindow.setPosition(pos); infoWindow.setContent(browserHasGeolocation ? 'Error: The Geolocation service failed.' : 'Error: Your browser doesn\'t support geolocation.'); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> </script> </body> </html>

μœ„μ˜ μ˜ˆμ‹œμ—μ„œ YOUR_API_KEYλŠ” Google Maps API ν‚€λ‘œ λŒ€μ²΄λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.