在 Mapbox 中通过折线移动的标记

Marker moving through polyline in Mapbox

我面临着一个真正的挑战,我希望你们能帮助我解决这个问题。 我正在为给定的 Position 对象创建一条多段线,而我想要做的是在原点放置一个自定义标记并使其在整个多段线上移动,不一定要跟踪位置,只是在多段线上移动。

我的第一步是创建一个 ObjectAnimation 对象,并使其在一条线上从一个标记移动到另一个标记,但我不知道如何让它沿着我的折线移动而不是在一条线上移动。

提前谢谢你,如果你们需要任何进一步的信息来澄清这个问题,我每次都会查看这个主题!

我们有一个example for this

您使用对象动画器是正确的,您还需要使用处理程序来不断更新位置。

// Animating the marker requires the use of both the ValueAnimator and a handler.
    // The ValueAnimator is used to move the marker between the GeoJSON points, this is
    // done linearly. The handler is used to move the marker along the GeoJSON points.
    handler = new Handler();
    runnable = new Runnable() {
      @Override
      public void run() {

        // Check if we are at the end of the points list, if so we want to stop using
        // the handler.
        if ((points.size() - 1) > count) {

          // Calculating the distance is done between the current point and next.
          // This gives us the duration we will need to execute the ValueAnimator.
          // Multiplying by ten is done to slow down the marker speed. Adjusting
          // this value will result in the marker traversing faster or slower along
          // the line
          distance = (long) marker.getPosition().distanceTo(points.get(count)) * 10;

          // animate the marker from it's current position to the next point in the
          // points list.
          ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
              new LatLngEvaluator(), marker.getPosition(), points.get(count));
          markerAnimator.setDuration(distance);
          markerAnimator.setInterpolator(new LinearInterpolator());
          markerAnimator.start();

          // This line will make sure the marker appears when it is being animated
          // and starts outside the current user view. Without this, the user must
          // intentionally execute a gesture before the view marker reappears on
          // the map.
          map.getMarkerViewManager().update();

          // Keeping the current point count we are on.
          count++;

          // Once we finish we need to repeat the entire process by executing the
          // handler again once the ValueAnimator is finished.
          handler.postDelayed(this, distance);
        }
      }
    };
    handler.post(runnable);