我正在创建安卓应用,它使用mapbox sdk显示地图中公交车的位置。 我想像Uber app一样根据位置旋转标记。 我怎么能实现这个目标?
代码:
IconFactory iconFactory = IconFactory.getInstance(navigationActivity.this);
Drawable iconDrawable = ContextCompat.getDrawable(navigationActivity.this, R.drawable.bus);
Icon icon = iconFactory.fromDrawable(iconDrawable);
map.clear();
CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(lat,lon)) // Sets the new camera position
.zoom(16) // Sets the zoom
.bearing(180) // Rotate the camera
.tilt(30) // Set the camera tilt
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory
.newCameraPosition(position), 7000);
final Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(lat,lon))
.title("You!")
.snippet("YOu are Currently here."));
marker.setIcon(icon);
答案 0 :(得分:3)
这里的an example除了公共汽车之外,还能满足您的要求,它实时跟踪国际空间站。使用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())
);
}
你也可以使用我之前在Turf之前使用过的方法:
// 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);
}