1

我已使用以下链接中的教程在 Android 应用程序中显示 Google 地图路线。我的问题是如何计算到地图上折线点的距离?就像当我使用谷歌地图应用程序时,它会告诉街道转弯何时接近。我想在我的应用程序中实现类似的功能。我能够在地图上显示路线折线,并且在我沿着它行驶时它会自行更新,但我希望它在即将到来的转弯前 500 英尺警告我。我怎样才能做到这一点?

链接在这里:

http://jigarlikes.wordpress.com/2013/04/26/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/

4

1 回答 1

0

我将这种方法用于标记。假设您有构成折线的点的纬度和经度,这应该这样做:

public class MapUtils {

public static float distBetween(LatLng pos1, LatLng pos2) {
    return distBetween(pos1.latitude, pos1.longitude, pos2.latitude,
            pos2.longitude);
}

/** distance in meters **/
public static float distBetween(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 a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)
            * Math.sin(dLng / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return (float) (dist * meterConversion);
}
}

为了确定道路是否在转弯,我会查看向量之间的欧几里德角度(x 是当前位置,y 是折线点)

为此,只需从前面一些距离获取您当前的位置和一个 LatLng。

计算基于:http ://en.wikipedia.org/wiki/Euclidean_space#Angle

Location currentLocation; // obtained somewhere in your code
LatLng polylinePoint; // a point further ahead

double cLat = currentLocation.getLatitude();
double cLon = currentLocation.getLongitude();

double pLat = polylinePoint.latitude;
double pLon = polylinePoint.longitude;

double angle = Math.acos(
        (cLat*pLat+cLon+pLon) / norm(cLat,cLon)*norm(pLat,cLon));

private double norm(double x, double y) {
    return Math.sqrt(Math.pow(x, 2)*Math.pow(y, 2));    
}

这是未经测试的,因此可能包含错误。

于 2013-10-20T18:42:14.883 回答