【问题标题】:Marker move on map but sometimes it is revolving at 360 degree in android标记在地图上移动,但有时它在 android 中以 360 度旋转
【发布时间】:2016-09-06 07:16:11
【问题描述】:

我现在面临一个问题,我正在开发一个类似于 Uber 或 lyft 的 Android 应用程序,但我面临的问题是汽车在地图上平稳移动,但有时它会在一个位置 360 度旋转,我需要停止,请帮忙,这是我的动画代码。

 private void animateMarkr(double laat, double lnng,Location prevLocation)
  {
    final LatLng toPosition=new LatLng(laat,lnng);
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = mGoogleMap.getProjection();
    Point startPoint = proj.toScreenLocation(carMarker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    final long duration = 3000;
    final boolean hideMarker=false;
    final Interpolator interpolator = new LinearInterpolator();
    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            LatLng pre=carMarker.getPosition();
            float t = interpolator.getInterpolation((float) elapsed / duration);
            double lng = t  toPosition.longitude + (1 - t)  startLatLng.longitude;
            double lat = t  toPosition.latitude + (1 - t)  startLatLng.latitude;
          //  carMarker.setRotation(getBearing(pre,new LatLng(lat,lng)));
            carMarker.setPosition(new LatLng(lat, lng));

            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 20);
            } else {

                if (hideMarker) {
                    carMarker.setVisible(false);
                } else {
                    carMarker.setVisible(true);
                }
            }
        }
    });
}

这是我的轮换代码

private void rotateMarker(Location location,Location preLocation){
    Location prevLocation = new Location(LocationManager.GPS_PROVIDER);
    prevLocation.setLatitude(carMarker.getPosition().latitude);
    prevLocation.setLongitude(carMarker.getPosition().longitude);
    final float toRotation = prevLocation.bearingTo(location);
           performRotaion(toRotation);
  }

private void performRotaion(final float toRotation){
    if(!isMarkerRotating) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final float startRotation = carMarker.getRotation();
        final float totalDegree=0;
        final long duration = 1000;

        final Interpolator interpolator = new LinearInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {
                isMarkerRotating = true;

                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed / duration);

                float rot = t  toRotation + (1 - t)  startRotation;

                if(carMarker != null){
                    carMarker.setAnchor(0.5f, 0.5f);
                    // if(rot<0.0f  && rot>-355.0f) {
                    carMarker.setRotation(-rot > 180 ? rot / 2 : rot);
                    carMarker.setFlat(true);
                }
                if (t < 1.0) {
                    handler.postDelayed(this, 16);
                } else {
                    isMarkerRotating = false;
                }
            }
        });
    }
}

// 方法调用

rotateMarker(location,previcsLocation);
animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);

如果度数是 360,我已经实施了停止旋转的检查,我还检查了度数是否超过 180 度,而不是将相同的度数除以该角度并移动汽车。但没有任何效果

【问题讨论】:

  • 我不确定你的标记行为的原因/原因,所以我能给你的是关于如何animate the rotation of the marker 的另一种方式。我希望它能给你一个关于如何解决这个问题的想法。
  • 嗨,Kendi,我用这个,效果不好,我使用不同的动画来移动汽车和旋转,但上面显示的动画很好,唯一的问题是有时标记旋转 360。我不知道是什么是问题所在。

标签: android google-maps-api-3 android-ndk marker


【解决方案1】:

在调用这些函数之前

rotateMarker(location,previcsLocation);
animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);

在您的代码中。如果两个值相同,则必须添加对来自服务器的新 (Latitude,Logitude) 和标记 (Latitude,Logitude) 的检查,那么您应该避免调用上述函数。

只有你必须在其中添加这个条件。

 LatLng new_location = new LatLng(data.getLatitude(), data.getLongitude());
 //new_location is coming through the server
 LatLng current_location = marker.getPosition();
 //this is the marker's location 
 if (current_location != new_location) {



if (current_location.equals(new_location)) {
    Log.d(TAG, "LatLng " + "---------current_location-" + current_location);
    Log.d(TAG, "LatLng " + "---------new_location-" + new_location);
    return;
}


rotateMarker(location,previcsLocation);
animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);

}

它在我的项目中有效。

【讨论】:

    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多