var location1
var location2
function AddDistance(Add1,Add2)
{
geocoder = new google.maps.Geocoder();
if (geocoder)
{
geocoder.geocode( { 'address': Add1}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
location1 = results[0].geometry.location;
}
});
geocoder.geocode( { 'address': Add2}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
location2 = results[0].geometry.location;
showMap();
}
});
}
}
function showMap()
{
latlng = new google.maps.LatLng((location1.lat()+location2.lat())/2,(location1.lng()+location2.lng())/2);
var myOptions = {
zoom: 1,
center:latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer(
{
suppressMarkers: true,
suppressInfoWindows: true
});
directionsDisplay.setMap(map);
var request = {
origin:location1,
destination:location2,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections(response);
}
});
// show a line between the two points
var line = new google.maps.Polyline({
map: map,
path: [location1, location2],
strokeWeight: 7,
strokeOpacity: 0.8,
strokeColor: "#FFAA00"
});
var line1 = new google.maps.Polyline({
map: map,
path: [myLatlng, location2],
strokeWeight: 7,
strokeOpacity: 0.8,
strokeColor: "#FFAA00"
});
// create the markers for the two locations
var marker1 = new google.maps.Marker({
map: map,
position: location1,
title: "Location1"
icon='icon name'
});
var marker2 = new google.maps.Marker({
map: map,
position: location2,
title: "AirPort",
icon: 'icon name'
});
var R = 6371;
var dLat = toRad(location2.lat()-location1.lat());
var dLon = toRad(location2.lng()-location1.lng());
var dLat1 = toRad(location1.lat());
var dLat2 = toRad(location2.lat());
var a1 = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(dLat1) * Math.cos(dLat1) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a1), Math.sqrt(1-a1));
var d = R * c;
// create the text to be shown in the infowindows
var text1 = '<b><font color=red>Distance</font></b> <br> : '+Math.floor(d)+' To '+Math.floor(d+1)+ 'Km'
var text2 = '<b><font color=red>Distance</font></b> <br> : '+Math.floor(d)+' To '+Math.floor(d+1)+ 'Km'
// create info boxes for the two markers
var infowindow1 = new google.maps.InfoWindow({
content: text1
});
var infowindow2 = new google.maps.InfoWindow({
content: text2
});
// add action events so the info windows will be shown when the marker is clicked
google.maps.event.addListener(marker1, 'click', function() {
infowindow1.open(map,marker1);
});
google.maps.event.addListener(marker2, 'click', function() {
infowindow2.open(map,marker2);
});
}
function toRad(deg)
{
return deg * Math.PI/180;
}