Calculating distance from KML

·—— ···· ·· — ····· ···—— ——···
I'd like to calculate the distance of my running route from its Google Maps representation.

The first step is to get the map in a programmable format, namely, KML. On the map, click the " KML" link to download the map in this XML format. The downloaded file contains a link to the "real" KML file. At first I got an {errorText:"Unable to contact server."} error from this second link, and I had to open the first KML file in Google Earth, then save the real info to KML from there. Now this direct link is working though.

Once you have the KML file, you need to parse out the coordinates of the line you're interested in, and then sum the distances of each segment of the line. Here's an initial attempt. I've assumed a square geometry since the distances are small, though in actuality the math is more complicated.

But we're not done yet. I measured this route at 5km/3.1m in my car, but my script tells me that it is only 4.07km/2.53m long. Here are some possible reasons for the discrepancy:
Anything else I could be missing?
·—— ···· ·· — ····· ···—— ——···
Feed back to Chad Whitacre.