Google Maps API has a route
function to do the same. Couldn't find anything like that in Google Earth API.
This is to be done on Linux, so won't be able to integrate it with Javascript API of Google maps.
Use the DirectionsAPI. It's linked from the Javascript Maps API documentation but is a REST API and can return results in XML.
Just make sure to check the Terms of Service to verify that your usage is allowed.