MapBox 中的标记方向 android
Marker direction in MapBox android
我正在创建 android 应用程序,它使用 mapbox sdk 在地图中显示公交车的位置。
我想像 Uber 应用程序一样根据位置旋转标记。
我怎样才能做到这一点?
代码:
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);
这里的 an example 可以满足您的要求,除了它不是公共汽车,它实时跟踪国际 Space 车站。标题的计算是使用 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);
}
我正在创建 android 应用程序,它使用 mapbox sdk 在地图中显示公交车的位置。 我想像 Uber 应用程序一样根据位置旋转标记。 我怎样才能做到这一点?
代码:
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);
这里的 an example 可以满足您的要求,除了它不是公共汽车,它实时跟踪国际 Space 车站。标题的计算是使用 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);
}