<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
function showMap()
{
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var map;
var mapProp =
{
mapTypeId : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('textDirection'));
var start = document.getElementById('origin').value;
var end = document.getElementById('destination').value;
var directionsRequest =
{
origin : start,
destination : end,
travelMode : google.maps.TravelMode.DRIVING
}
directionsService.route(directionsRequest, function(response, status)
{
if(status === google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections(response);
}
else
{
window.alert('Cannot');
}
});
}
function resetMap()
{
document.getElementById("origin").value = '';
document.getElementById("destination").value = '';
document.getElementById("googleMap").style.display = 'none';
document.getElementById("textDirection").style.display = 'none';
}
</script>
<style>
#textDirection{
width: 300px;
height: 62%;
float: right;
overflow-y: auto;
margin-top: 1%;
margin-right: 29%;
}
#googleMap{
width : 50%;
height : 60%;
position : absolute;
top: 60px;
margin-left: 5px;
}
</style>
</head>
<body>
<form action="showmap1.php" method="post">
From: <input type="text" name="origin" id="origin" size="30" />
To:<input type="text" name="destination" id="destination" size="30" />
<input type="button" value="Search" onclick="showMap()" />
<input type="button" value="Reset" onclick="resetMap()" />
<div id="googleMap"></div>
<div id="textDirection"></div>
</form>
</body>
</html>
From above code, I have following question:
(1) When I clicked Reset button, I search another place and click Search button. However, the google map and text direction doesn't show like the below image.
How should I modify it?
(2) When I search the location 2nd time, the text direction route are duplicated show like below image.
How should I make it only show once?