0

我有包含路线信息的 KML 文件。现在我需要知道这条路线的长度(以公里为单位)。我看到 tag 的 KML 文件中有这个长度,看起来像这样:

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

但在我看来,解析这个字符串是错误的方式。将不胜感激任何替代和/或建议!谢谢!

4

1 回答 1

2

我认为最好的解决方案是使用路线点的坐标来计算路线长度。这是我解决这个问题的代码:

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();
}
于 2012-05-23T09:35:12.260 回答