我如何计算在Android的谷歌地图V2当前距离折线点?折线、距离、地图、Android

2023-09-07 23:12:18 作者:降妞十巴掌

我已经使用教程,从下面的链接以显示Android应用谷歌地图的路线。我的问题是如何计算距离,折线点在地图上?就像当我使用谷歌地图应用程序,它在什么时候一条街又是越来越接近。我想在我的应用程序来实现类似的功能。我能够在地图上显示的路线折线,并更新自己,而我也一起开车,但我希望它提醒我500英尺提前的到来转弯。我该怎么办呢?

I have used tutorial from the link below to display Google map route in Android app. My question is how can I calculate distance to polyline points on map? Like when I use Google maps app and it tells when a street turn is getting close. I want to implement similar feature in my app. I am able to display the route polyline on the map and it updates itself while I drive along it but I want it to warn me 500 feet in advance of a coming turn. How can I do that?

下面是链接:

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

推荐答案

我用这个方法进行标记。假设你有弥补的折线,这应该做点经纬度:

I use this method for Markers. Assuming you have Latitude and Longitude of the points that make up the Polyline this should do:

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是一条折线点)

To determine wether the road is turning, I would look into euclidean angle between vectors (x being current location and y being a polyline point)

简单地从远处把你的当前位置和经纬度提前这一点。

Simply take your current location and a LatLng from some distance ahead for this.

的计算是基于: 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));    
}

这是未经测试,可能会包含错误。

This is untested so might contain error.