mapboxandroid中的标记方向

mapboxandroid中的标记方向,android,mapbox,Android,Mapbox,我正在创建一个android应用程序,它使用mapbox sdk在地图中显示一辆公交车的位置。 我想根据位置旋转标记,就像Uber应用程序一样。 我怎样才能做到这一点 代码: 这是一个可以满足你要求的系统,它可以实时跟踪国际空间站,而不是公交车。标题的计算是使用Turf和Mapbox Android Services SDK完成的,但如果您只需要该单一方法,则可以从库中复制该方法。下面是我上面提到的示例中的重要代码: // Make sure you are using marker views

我正在创建一个android应用程序,它使用mapbox sdk在地图中显示一辆公交车的位置。 我想根据位置旋转标记,就像Uber应用程序一样。 我怎样才能做到这一点

代码:

这是一个可以满足你要求的系统,它可以实时跟踪国际空间站,而不是公交车。标题的计算是使用Turf和Mapbox Android Services SDK完成的,但如果您只需要该单一方法,则可以从库中复制该方法。下面是我上面提到的示例中的重要代码:

// Make sure you are using marker views so you can update the rotation.
marker.setRotation((float) computeHeading(marker.getPosition(), position));

...

public static double computeHeading(LatLng from, LatLng to) {
// Compute bearing/heading using Turf and return the value.
    return TurfMeasurement.bearing(
        Position.fromCoordinates(from.getLongitude(), from.getLatitude()),
        Position.fromCoordinates(to.getLongitude(), to.getLatitude())
    );
}
您也可以使用我以前在使用人造草坪之前使用的方法:

// Returns the heading from one LatLng to another LatLng. Headings are. Expressed in degrees
// clockwise from North within the range [-180,180). The math for this method came from
// http://williams.best.vwh.net/avform.htm#Crs I only converted it to Java.
public static double computeHeading(LatLng from, LatLng to) {
    double fromLat = Math.toRadians(from.getLatitude());
    double fromLng = Math.toRadians(from.getLongitude());
    double toLat = Math.toRadians(to.getLatitude());
    double toLng = Math.toRadians(to.getLongitude());
    double dLng = toLng - fromLng;
    double heading = Math.atan2(Math.sin(dLng) * Math.cos(toLat),
            Math.cos(fromLat) * Math.sin(toLat) - Math.sin(fromLat) * Math.cos(toLat) * Math.cos(dLng));
    return (Math.toDegrees(heading) >= -180 && Math.toDegrees(heading) < 180) ?
            Math.toDegrees(heading) : ((((Math.toDegrees(heading) + 180) % 360) + 360) % 360 + -180);
}
这是一个可以满足你要求的系统,它可以实时跟踪国际空间站,而不是公交车。标题的计算是使用Turf和Mapbox Android Services SDK完成的,但如果您只需要该单一方法,则可以从库中复制该方法。下面是我上面提到的示例中的重要代码:

// Make sure you are using marker views so you can update the rotation.
marker.setRotation((float) computeHeading(marker.getPosition(), position));

...

public static double computeHeading(LatLng from, LatLng to) {
// Compute bearing/heading using Turf and return the value.
    return TurfMeasurement.bearing(
        Position.fromCoordinates(from.getLongitude(), from.getLatitude()),
        Position.fromCoordinates(to.getLongitude(), to.getLatitude())
    );
}
您也可以使用我以前在使用人造草坪之前使用的方法:

// Returns the heading from one LatLng to another LatLng. Headings are. Expressed in degrees
// clockwise from North within the range [-180,180). The math for this method came from
// http://williams.best.vwh.net/avform.htm#Crs I only converted it to Java.
public static double computeHeading(LatLng from, LatLng to) {
    double fromLat = Math.toRadians(from.getLatitude());
    double fromLng = Math.toRadians(from.getLongitude());
    double toLat = Math.toRadians(to.getLatitude());
    double toLng = Math.toRadians(to.getLongitude());
    double dLng = toLng - fromLng;
    double heading = Math.atan2(Math.sin(dLng) * Math.cos(toLat),
            Math.cos(fromLat) * Math.sin(toLat) - Math.sin(fromLat) * Math.cos(toLat) * Math.cos(dLng));
    return (Math.toDegrees(heading) >= -180 && Math.toDegrees(heading) < 180) ?
            Math.toDegrees(heading) : ((((Math.toDegrees(heading) + 180) % 360) + 360) % 360 + -180);
}

你没有提到你面临的问题!您已经在codeYeah中实现了方向角和倾斜功能。当地图加载时,它将设置动画并旋转。。但是,当另一个位置出现在另一条水平道路上时,巴士图标将进入该道路,但垂直方向是标记图像的方向。。我需要它水平对齐@Stallion你没有提到你面临的问题!您已经在codeYeah中实现了方向角和倾斜功能。当地图加载时,它将设置动画并旋转。。但是,当另一个位置出现在另一条水平道路上时,巴士图标将进入该道路,但垂直方向是标记图像的方向。。我需要它水平对齐@Stallion我尝试了第二个函数,但它仍然没有指向方向..嘿,我的错误,从和到我给出了相同的坐标。。现在它起作用了。。但我现在面临的另一个问题是,每次标记改变时,它都会旋转,即使它是朝同一个方向旋转。它不应该是,每次都旋转。上面代码的唯一问题是它总是顺时针旋转标记,即使逆时针旋转会更短。解决这个问题的方法是通过一个if检查来确定旋转的方向。是的,我最终找到了一个工作,在哪里而不是标记我旋转了地图,所以地图不会一直旋转,除非有方向改变。。无论如何,谢谢。我尝试了第二个函数,但它仍然没有指向方向。嘿,我的错误,从和到我给出了相同的坐标。。现在它起作用了。。但我现在面临的另一个问题是,每次标记改变时,它都会旋转,即使它是朝同一个方向旋转。它不应该是,每次都旋转。上面代码的唯一问题是它总是顺时针旋转标记,即使逆时针旋转会更短。解决这个问题的方法是通过一个if检查来确定旋转的方向。是的,我最终找到了一个工作,在哪里而不是标记我旋转了地图,所以地图不会一直旋转,除非有方向改变。。无论如何,谢谢。