Code:
<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Event Closure</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<!-- <link rel="stylesheet" type="text/css" media="screen" href="videomap.css" />
-->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map, directionDisplay, directionsService, stepDisplay, markerArray = [], distanceArray = [], TimeArray = [], end = [];
end[0] = "Ithaca, New York, United States";
end[1] = "Fountain Valley, California, US";
end[2] = "Los Angeles, California, US";
end[3] = "San Francisco, California, US";
end[4] = "Huntsville, Alabama, US";
end[5] = "Atlanta, Georgia, US";
end[6] = "Berlin, Germany";
end[7] = "Boston, Massachusetts, US";
end[8] = "Chicago, Illinois, us";
end[9] = "Columbus, Ohio, US";
end[10] = "Detroit, Michigan, us";
end[11] = "Des Moines, Iowa, US";
function initialize() {
var myOptions = {
zoom: 10,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(
document.getElementById("map_canvas"), myOptions);
setMarkers(map, videographer);
directionsService = new google.maps.DirectionsService();
var rendererOptions = {
map: map
}
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions)
// Instantiate an info window to hold step text.
stepDisplay = new google.maps.InfoWindow();
var opt = document.createElement('option'), topt;
for(var i = 0; i < end.length; ++i){
topt = opt.cloneNode(false);
topt.value = end[i];
topt.appendChild(document.createTextNode(end[i]));
document.getElementById('new_address').appendChild(topt);
}
}
var videographer = [
['One', 42.4433333, -76.5, 1],
['Two', 33.7091847, -117.9536697, 2],
['Three', 34.0522342, -118.2436849, 3],
['FOur', 37.7749295, -122.4194155, 4],
['Five', 34.7303688, -86.5861037, 5],
['Six', 33.7489954, -84.3879824, 6],
['Seven', 52.5234051, 13.4113999, 7],
['eight', 42.3584308, -71.0597732, 8],
['Nine', 41.850033, -87.6500523, 9],
['ten', 39.9611755, -82.9987942, 10],
['eleven', 42.331427, -83.0457538, 11],
['twelve', 41.6005448, -93.6091064, 12]
];
function setMarkers(map, locations) {
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < locations.length; i++) {
var videographer = locations[i], myLatLng = new google.maps.LatLng(videographer[1], videographer[2]), marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: videographer[0]
});
bounds.extend(myLatLng);
map.fitBounds(bounds);
}
}
function calcRoute(a) {
var request = {
origin: document.getElementById('new_address').value,
destination: end[a],
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, output = document.getElementById('output'), r = '';
if(a === 0){
output.innerHTML = ' ';
}
directionsService.route(request, function(response, status){
if (response.status === 'OK'){
distanceArray[a] = (response.routes[0].legs[0].duration.value * .000621371192) + ' Miles';
TimeArray[a] = response.routes[0].legs[0].duration.text;
} else {
distanceArray[a] = 9999999;
TimeArray[a] = 'Never';
}
if(++a < end.length){
calcRoute(a);
} else {
for (a = 0; a < distanceArray.length; ++a) {
if (distanceArray[a]){
r += (a? '<br />\n' : '') + a + '. ' + distanceArray[a];
}
}
output.innerHTML = r;
}
});
}
function showSteps(directionResult) {
// For each step, place a marker, and add the text to the marker's
// info window. Also attach the marker to an array so we
// can keep track of it and remove it when calculating new
// routes.
var myRoute = directionResult.routes[0].legs[0];
for (var i = 0; i < myRoute.steps.length; i++) {
var marker = new google.maps.Marker({
position: myRoute.steps[i].start_point,
map: map
});
markerArray[i] = marker;
}
}
</script>
</head>
<body onload="initialize()">
<div id="form" style="width:300px; float:left; height:50px;">
<select name="new_address" id="new_address"></select>
<input type="Submit" value="Find Videographer" onclick="calcRoute(0)" />
</div>
<div id="output" style="background:#FF0000; width:200px; float:left;"> </div>
<div id="map_canvas" style="width: 500px; height: 500px; float:left; border:3px #000000 solid; margin-left:20px;"></div>
<div id="warnings_panel"></div>
</body>
</html>
Bookmarks