【问题标题】:Marker direction in MapBox androidMapBox android中的标记方向
【发布时间】:2016-09-16 10:05:51
【问题描述】:

我正在创建 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);

【问题讨论】:

  • 你没有提到你面临什么问题!您已经在代码中实现了方位和倾斜功能
  • 是的,当加载地图时,它会动画和旋转。但是当另一个位置进入另一个水平道路时,公共汽车图标会去那条道路但垂直方向,标记的图像如何是..我需要它水平对齐@Stallion

标签: android mapbox


【解决方案1】:

这里是an example,它可以满足您的要求,但不是公共汽车,而是实时跟踪国际空间站。航向的计算是使用 Turf 和 Mapbox Android 服务 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);
}

【讨论】:

  • 我尝试了第二个功能仍然没有指向方向..
  • 嘿,我的错误,from 和 to 我都给出了相同的坐标。同一个方向..
  • 不应该,每次都旋转。上面代码的唯一问题是它总是顺时针旋转标记,即使逆时针旋转会更短。解决此问题的方法是进行 if 检查以确定要旋转的方向。
  • 是的,我终于找到了一个解决方法,而不是标记我旋转了地图,所以除非有方向改变,否则地图不会一直旋转。无论如何谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多