Skip to content

Instantly share code, notes, and snippets.

@arnellebalane
Created November 29, 2015 07:40
Show Gist options
  • Save arnellebalane/90cf03171982748d9c12 to your computer and use it in GitHub Desktop.
Save arnellebalane/90cf03171982748d9c12 to your computer and use it in GitHub Desktop.
html5 geolocation api + google maps directions api to display user location and the name of the nearest road
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Geolocation API</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBdhHU9eaIJBJfoKj2fnpGRIJdcGBz07Qg&callback=displayMap" defer async></script>
<script src="main.js"></script>
</body>
</html>
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
height: 100vh;
}
function displayMap() {
navigator.geolocation.getCurrentPosition(function(position) {
var coordinates = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var map = new google.maps.Map(document.body, {
center: coordinates,
zoom: 17
});
var direction = new google.maps.DirectionsService();
direction.route({
origin: coordinates,
destination: coordinates,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function (response, status) {
var marker = new google.maps.Marker({
position: coordinates,
title: response.routes[0].legs[0].start_address,
map: map
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment