I've a form with three fields hospital name,city & Your Location when the user hits submit button, he navigates to a new page displaying map along with directions. Route directions are not displaying. I'm unable to figure out mistake.Any help will be appreciated.
CSS:
html,
body,
#container {
margin: 0;
padding: 0;
height: 700px;
width: 1350px;
}
#sidebar {
float: right;
width: 250px;
height: 100%;
background: white;
border: 0px solid #DDD;
overflow: auto;
overflow-x: hidden;
z-index: 30;
box-shadow: -1px 1px 3px -1px #000;
}
#map-canvas {
float: left;
width: 1100px;
height: 700px;
}
.row {
padding: 10px;
}
.separator {
width: 96%;
height: 1px;
margin: 0 auto;
border-bottom: 1px solid black;
}
label {
font: 12px/14px Arial, Helvetica, sans-serif;
font-weight: bold;
}
JS:
< script src = "http://maps.googleapis.com/maps/api/js??key=AIzaSyBcM1JmHJLyPH6m5xF0WzrqWJ9pSjCTQR4&sensor=false" / >
<script>
var loc = '<%= Session["hname"] %>';
var city = '<%= Session["city"] %>';
var userloc = '<%= Session["userloc"] %>';
var locations = [loc + city];
var geocoder;
var map;
var marker;
var markersArray = [];
var bounds;
var array = [];
var directionsDisplay;
var directionsService;
function initialize() {
geocoder = new google.maps.Geocoder();
bounds = new google.maps.LatLngBounds();
latlang = geocoder.geocode({
'address': userloc
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
array.push([results[0].geometry.location, results[0].geometry.location]);
var contentString = '<div id="content">' +
'Lat :' + results[0].geometry.location.lat() + '</br>lng :' + results[0].geometry.location.lng() +
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: results[0].geometry.location.lat() + ' , ' + results[0].geometry.location.lng()
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
markersArray.push(marker);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
var myOptions = {
center: latlang,
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
};
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
map = new google.maps.Map(document.getElementById("map-canvas"),
myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('sidebar'));
var request = {
origin: userloc,
destination: loc,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function plotMarkers() {
for (var i = 0; i < locations.length; i++) {
codeAddresses(locations[i]);
}
directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
directionsDisplay.setMap(map);
var request = {
origin: userloc,
destination: loc,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function codeAddresses(address) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
array.push([results[0].geometry.location, results[0].geometry.location]);
var contentString = '<div id="content">' +
'Lat :' + results[0].geometry.location.lat() + '</br>lng :' + results[0].geometry.location.lng() +
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: results[0].geometry.location.lat() + ' , ' + results[0].geometry.location.lng()
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
markersArray.push(marker);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
< /script>
HTML:
<!DOCTYPE html>
<body onload="initialize()">
<form id="form2" runat="server">
<div id="container">
<div id="map-canvas">
<div id="sidebar">
<label>Get Directions</label>
<div class="separator"></div>
<div id="divDirections">
</div>
</div>
</div>
</div>
</form>
</body>
</html>