简体   繁体   中英

How can i get route length from KML file?

I have KML file with route information. Now i need to know length of this route in kilometers. I see that there is this length in KML file in tag , wich looks like this:

<description><![CDATA[Distance: 750&#160;m (about 2 mins)<br/>Map data ©2012 Google]]></description>

But it seems to me that parsing this string is wrong way. Would be grateful for any alternatives and/or suggestions! Thanks!

I think the best solution is to calculate route length using coordinates of route points. Here is my code solving this problem:

ArrayList<GeoPoint> points;
private static final int EARTH_RADIUS = 6371;

.....
            for(int i = 0; i < (points.size() - 1); ++i) {
               final double lat1 = toRadians(points.get(i).getLatitudeE6() / LocationHelper.MILLION);
               final double lat2 = toRadians(points.get(i + 1).getLatitudeE6() / LocationHelper.MILLION);
               final double lon1 = toRadians(points.get(i).getLongitudeE6() / LocationHelper.MILLION);
               final double lon2 = toRadians(points.get(i + 1).getLongitudeE6() / LocationHelper.MILLION);

               length += acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(lon2 - lon1)) * EARTH_RADIUS;
            }
        road.length = round(length, 2, BigDecimal.ROUND_HALF_UP);

....
private static double round(double unrounded, int precision, int roundingMode)
{
    final BigDecimal bd = new BigDecimal(unrounded);
    final BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM