如何在android Map v2中显示地图距离文字

时间:2014-01-22 06:09:14

标签: android google-maps

我试图用(以km为单位)显示距离地图。我实现了这个链接的代码Find distance between two points on map using Google Map API V2 - Abhijit Kurane

public double CalculiationByDistance(LatLng StartP, LatLng EndP) {
    int Radius = 6371;
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
            + " Meter   " + meterInDec);

    return Radius * c;
}

但是我只有错误的距离,logcat只有0.2公里的距离。 我该怎么办?

提前致谢..

1 个答案:

答案 0 :(得分:0)

public static double distFrom(double lat1, double lng1, double lat2,
            double lng2) {
        double earthRadius = 3958.75;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLng = Math.toRadians(lng2 - lng1);
        double sindLat = Math.sin(dLat / 2);
        double sindLng = Math.sin(dLng / 2);
        double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
                * Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2));
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double dist = earthRadius * c;

        return dist;
    }


use as in KM like ::-->

distance = distFrom(latitude, longitude, lat_array.get(i),
                            long_array.get(i));
double km = distance / 0.62137;