Skip to content

Instantly share code, notes, and snippets.

@burhr2
Forked from theredstapler/google-map-traffic.html
Created February 28, 2018 20:53
Show Gist options
  • Save burhr2/dff0d923fc2301e31cfb214b9ffdb192 to your computer and use it in GitHub Desktop.
Save burhr2/dff0d923fc2301e31cfb214b9ffdb192 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="jquery-2.1.4.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
<style>
#map {
height: 500px;
}
</style>
</head>
<body>
<div id="navbar"><span>Google Maps - Javascript API</span></div>
<div id="wrapper">
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 40.797, lng: -73.9644},
zoom: 13
});
var trafficLayer = new google.maps.TrafficLayer();
var transitLayer = new google.maps.TransitLayer();
var bikeLayer = new google.maps.BicyclingLayer();
$('#traffic-b').click(function(){
trafficLayer.setMap(map);
transitLayer.setMap(null);
bikeLayer.setMap(null);
});
$('#transit-b').click(function(){
trafficLayer.setMap(null);
transitLayer.setMap(map);
bikeLayer.setMap(null);
});
$('#bike-b').click(function(){
trafficLayer.setMap(null);
transitLayer.setMap(null);
bikeLayer.setMap(map);
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"
async defer></script>
<button id="traffic-b">Traffic</button>
<button id="transit-b">Transit</button>
<button id="bike-b">Bike</button>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment