Это функция, которую я использую на своем сайте (вместе с небольшим количеством jQuery:
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
function setMap(midpoint)
{
var latlng = new google.maps.LatLng(midpoint.lat(), midpoint.lng());
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
directionsDisplay.setMap(map);
var request = {
origin:"[Put origin here]",
destination:"[Put destination here]",
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
function initialize() {
var address = "[Put destination here]";
var geocoder = new google.maps.Geocoder();
directionsDisplay = new google.maps.DirectionsRenderer();
var result = "";
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
result = results[0].geometry.location;
setMap(result);
}
else
{
$("#map_canvas").hide();
}
});
}