cartodb-4.42/lib/assets/javascripts/cdb/examples/tutorial-google-driving-2.html
2024-04-06 05:25:13 +00:00

75 lines
2.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Driving directions to clicked point | CartoDB.js</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="https://cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<!-- include google maps library -->
<script type="text/javascript" src="http://www.maps.google.com/maps/api/js?sensor=false&v=3.30"></script>
<!-- include cartodb.js library -->
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
<script>
var map;
var google_api_key = "AIzaSyCADWr4a6NraGN1ldmcBKN4W_6c6teuImw"
function main() {
// Map center
var myLatlng = new google.maps.LatLng(37.753, -122.433);
var myOptions = {
zoom: 13,
center: myLatlng,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
// Render basemap
map = new google.maps.Map(document.getElementById("map"), myOptions);
// Create services for later rendering of directions
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
var directionsService = new google.maps.DirectionsService();
// The location of the Exploratorium
var exploratorium = new google.maps.LatLng(37.801434, -122.397561);
// Our CartoDB visualization
var vizjson_url = "https://documentation.cartodb.com/api/v2/viz/4a885510-d6fb-11e4-aedb-0e4fddd5de28/viz.json";
cartodb.createLayer(map, vizjson_url)
.addTo(map)
.done(function(layers) {
var subLayer = layers.getSubLayer(0);
subLayer.setInteraction(true); // Interaction for that layer must be enabled
// Setup our event when an object is clicked
layers.on('featureClick', function(e, latlng, pos, data){
// the location of the clicked school
var school = new google.maps.LatLng(latlng[0], latlng[1]);
var request = {
origin : school,
destination : exploratorium,
travelMode : google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
});
});
}
window.onload = main;
</script>
</body>
</html>