Created
November 29, 2015 07:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*, | |
*::before, | |
*::after { | |
margin: 0; | |
padding: 0; | |
box-sizing: border-box; | |
} | |
html, | |
body { | |
height: 100vh; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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