Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Showing User's Location on Google Map</title> <script src="https://maps.google.com/maps/api/js?sensor=false"></script> <script> function showPosition() { if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(showMap, showError); } else { alert("Sorry, your browser does not support HTML5 geolocation."); } } // Define callback function for successful attempt function showMap(position) { // Get location data lat = position.coords.latitude; long = position.coords.longitude; var latlong = new google.maps.LatLng(lat, long); var myOptions = { center: latlong, zoom: 16, mapTypeControl: true, navigationControlOptions: { style:google.maps.NavigationControlStyle.SMALL } } var map = new google.maps.Map(document.getElementById("embedMap"), myOptions); var marker = new google.maps.Marker({ position:latlong, map:map, title:"You are here!" }); } // Define callback function for failed attempt function showError(error) { if(error.code == 1) { result.innerHTML = "You've decided not to share your position, but it's OK. We won't ask you again."; } else if(error.code == 2) { result.innerHTML = "The network is down or the positioning service can't be reached."; } else if(error.code == 3) { result.innerHTML = "The attempt timed out before it could get the location data."; } else { result.innerHTML = "Geolocation failed due to unknown error."; } } </script> </head> <body> <button type="button" onclick="showPosition();">Show My Position on Google Map</button> <div id="embedMap" style="width: 400px; height: 300px;"> <!--Google map will be embedded here--> </div> </body> </html>