【问题标题】:Rotate marker/car icon in google maps - Android在谷歌地图中旋转标记/汽车图标 - Android
【发布时间】:2017-01-04 10:19:33
【问题描述】:

我正在尝试创建一个像 ola/uber 这样的应用程序。我想在道路左转或右转时移动图标并旋转。我正在使用以下代码。

private void rotateMarker(final Marker marker, final float toRotation) {
        if(!isMarkerRotating) {
            final Handler handler = new Handler();
            final long start = SystemClock.uptimeMillis();
            final float startRotation = marker.getRotation();
            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;

                    marker.setRotation(-rot > 180 ? rot / 2 : rot);
                    if (t < 1.0) {
                        // Post again 16ms later.
                        handler.postDelayed(this, 16);
                    } else {
                        isMarkerRotating = false;
                    }
                }
            });
        }
    }

计算方位:

        currentLocation = location;
        if(previousLocaton!=null){
            previousLocaton = tempLocation;
            tempLocation = currentLocation;

            Log.d("previousLocaton=====> ",""+previousLocaton);
            Log.d("currentLocation=====> ",""+currentLocation);

            bearing = previousLocaton.bearingTo(currentLocation) ;
        }else{
            previousLocaton = location;
            tempLocation = location;
        }

设置方位:

CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(latLng).zoom(14).bearing(bearing).build();

要旋转标记,我在onLocationChanged 中调用了roateMarker 方法更改了方法:

        currLocationMarker = mMap.addMarker(markerOptions);
        rotateMarker(currLocationMarker,bearing);

现在我的图标正在旋转。但谷歌地图也会旋转。我想单独旋转图标。我参考以下链接动画和移动标记。 Link 1。请让我有任何想法来解决我的问题。

【问题讨论】:

    标签: android google-maps rotation


    【解决方案1】:

    标记的方法很简单

    marker.rotation(float value)
    

    设置标记绕标记锚点顺时针旋转的度数。旋转轴垂直于标记。旋转 0 对应于标记的默认位置。当标记在地图上平放时,默认位置为北对齐,并且旋转使得标记始终在地图上保持平放。当标记是广告牌时,默认位置是向上的,并且旋转使得标记始终面向相机。默认值为 0。

    【讨论】:

    • 嗨..对不起,长时间的休息..这也很好。但我面临另一个问题。我每 5 秒更换一次标记。当我更换标记时,标记位置从它的真实位置开始。例如,我的标记是默认的箭头标记,它表示顶部天使。当我从一个地方移动到另一个地方时,我的标记会旋转,突然它从顶部天使开始。请让我有任何想法来解决这个问题。
    • 嗨!我为此使用线程。我跟踪标记位置并将其存储在我的数据库中。在谷歌地图片段上,我从我的数据库中获取标记最后一个位置并将其用作位置 2(这意味着下一次获取这将是位置 1)
    【解决方案2】:

    使用setRotation(float) 方法仅旋转标记集旋转到标记。

    static public void rotateMarker(final Marker marker, final float toRotation) {
            final Handler handler = new Handler();
            final long start = SystemClock.uptimeMillis();
            final float startRotation = marker.getRotation();
            final long duration = 1000;
    
            final Interpolator interpolator = new LinearInterpolator();
            L.d("Bearing: "+toRotation);
    
            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float t = interpolator.getInterpolation((float) elapsed / duration);
    
                    float rot = t * toRotation + (1 - t) * startRotation;
                    marker.setRotation(-rot > 180 ? rot / 2 : rot);
                    if (t < 1.0) {
                        // Post again 10ms later.
                        handler.postDelayed(this, 10);
                    }
                }
            });
        }
    

    【讨论】:

    • 是的,只要去掉这段代码 CameraPosition cameraPosition = new CameraPosition.Builder() .target(latLng).zoom(14).bearing(bearing).build();
    • 如果您不想旋转地图而是更新位置,则使用此代码代替 CameraPosition cameraPosition = new CameraPosition.Builder() .target(latLng).zoom(14).build();
    • 嗨..对不起,长时间休息..它工作正常。但我面临另一个问题。我每 5 秒更换一次标记。当我更换标记时,标记位置从它的真实位置开始。例如,我的标记是箭头标记,默认表示顶部天使。当我从一个地方移动到另一个地方时,我的标记会旋转,突然它从顶部天使开始。请告诉我解决此问题的任何想法。
    • 您可以将锚点设置为标记。尝试像这样设置 marker.setAnchor(0.5f, 0.5f);
    【解决方案3】:

    试试这个:

    public void animateMarker(final LatLng toPosition, final LatLng startLatLng,
                              final boolean hideMarker) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        Projection proj = map.getProjection();
        Point startPoint = proj.toScreenLocation(d_marker.getPosition());
        final LatLng startLatLng = proj.fromScreenLocation(startPoint);
        //   final CameraPosition newcameraPosition = null;
    
        final Interpolator interpolator = new LinearInterpolator();
    
        handler.post(new Runnable() {
            @Override
            public void run() {
    
                Location prevLoc = new Location("service Provider");
                prevLoc.setLatitude(startLatLng.latitude);
                prevLoc.setLongitude(startLatLng.longitude);
    
                Location newLoc = new Location("service Provider");
                newLoc.setLatitude(toPosition.latitude);
                newLoc.setLongitude(toPosition.longitude);
    
                System.out.println("Locations ---- " + prevLoc + "-" + newLoc);
    
                float bearing = prevLoc.bearingTo(newLoc);
    
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / 1000);
                double lng = t * toPosition.longitude + (1 - t)
                        * startLatLng.longitude;
                double lat = t * toPosition.latitude + (1 - t)
                        * startLatLng.latitude;
                d_marker.setPosition(new LatLng(lat, lng));
                d_marker.setRotation(bearing);
                d_marker.setFlat(true);
                //   googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(newcameraPosition));
    
    
                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                } else {
                    if (hideMarker) {
                        d_marker.setVisible(false);
                    } else {
                        d_marker.setVisible(true);
                    }
                }
            }
        });
    
    
    }
    

    【讨论】:

      【解决方案4】:

      试试下面的方法:

       public void animateMarker(final LatLng toPosition,
                                final boolean hideMarker) {
          final Handler handler = new Handler();
          final long start = SystemClock.uptimeMillis();
          Projection proj = googleMap.getProjection();
          Point startPoint = proj.toScreenLocation(cabMarker.getPosition());
          final LatLng startLatLng = proj.fromScreenLocation(startPoint);
       //   final CameraPosition newcameraPosition = null;
      
          final Interpolator interpolator = new LinearInterpolator();
      
          handler.post(new Runnable() {
              @Override
              public void run() {
      
                  Location prevLoc = new Location("service Provider");
                  prevLoc.setLatitude(startLatLng.latitude);
                  prevLoc.setLongitude(startLatLng.longitude);
      
                  Location newLoc = new Location("service Provider");
                  newLoc.setLatitude(toPosition.latitude);
                  newLoc.setLongitude(toPosition.longitude);
      
                  System.out.println("Locations ---- " + prevLoc + "-" + newLoc);
      
                  float bearing = prevLoc.bearingTo(newLoc);
      
                  long elapsed = SystemClock.uptimeMillis() - start;
                  float t = interpolator.getInterpolation((float) elapsed
                          / 1000);
                  double lng = t * toPosition.longitude + (1 - t)
                          * startLatLng.longitude;
                  double lat = t * toPosition.latitude + (1 - t)
                          * startLatLng.latitude;
                  cabMarker.setPosition(new LatLng(lat, lng));
      
      
                  cabMarker.setRotation(bearing);
               //   googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(newcameraPosition));
      
      
                  if (t < 1.0) {
                      // Post again 16ms later.
                      handler.postDelayed(this, 16);
                  } else {
                      if (hideMarker) {
                          cabMarker.setVisible(false);
                      } else {
                          cabMarker.setVisible(true);
                      }
                  }
              }
          });
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-20
        • 2016-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-15
        • 1970-01-01
        • 2014-08-25
        • 1970-01-01
        相关资源
        最近更新 更多