【问题标题】:How to smoothly keep moving current location marker in Google Maps v2 android如何在 Google Maps v2 android 中平稳地保持移动当前位置标记
【发布时间】:2014-11-23 08:32:02
【问题描述】:

我已经为我的位置放置了一个标记。我想像谷歌地图应用程序一样平稳地移动我的标记。当我继续在我的车里移动时,蓝色圆圈移动得非常顺利。我想为我的应用程序实现相同的功能。如何在我的应用程序上实现这一点?

到目前为止,我的位置标记只是在位置变化时跳转到不同的位置并在那里标记标记。

这是我所拥有的:

 googleMap.setMyLocationEnabled(true);

所以会自动调用 onMyLocationChange 方法:

@Override
public void onMyLocationChange(Location location) 
{
    curlat = location.getLatitude();
    curlong = location.getLongitude();

    if (markermylocation != null)
    {
        markermylocation.remove();
    }

        curlat = location.getLatitude();
        curlong = location.getLongitude();
        myLocation(curlat, curlong, username, imageURL, ZOOMVALUE);
}

在 mylocaiton 方法中:

private void myLocation(double lat, double lng, String name, String url, float zoom)
{
    if(firsttime  == 1)
    {
        LatLng ll = new LatLng(lat,lng);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
        googleMap.animateCamera(update);
        firsttime = 0;
    }

    final String uname = name; 
    curlat = lat;
    curlong = lng;
    LatLng position = new LatLng(curlat, curlong);

    markerOptionsmylocaiton = new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromBitmap(icon)).title(uname).anchor(0.5f, 1f);
    markermylocation = googleMap.addMarker(markerOptionsmylocaiton);

    LatLng latlang = new LatLng(curlat, curlong);
    animateMarker(markermylocation, latlang, false);
}

因此,每次调用 mylocation 时,都会删除标记并调用 mylocation 方法,然后在新位置创建标记。相反,我想像谷歌地图一样平滑过渡标记?如何实现?

更新:

在为此工作了几次之后:我终于来到了这段代码,但是我的标记没有显示出来:

我正在使用下面的方法,并且每次都在 myLocation 方法中调用它。

public void animateMarker(final Marker marker, final LatLng toPosition,
        final boolean hideMarker) 
{
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = googleMap.getProjection();
    Point startPoint = proj.toScreenLocation(marker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    final long duration = 500;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() 
    {
        @Override
        public void run() 
        {
            long elapsed = SystemClock.uptimeMillis() - start;
            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;
            marker.setPosition(new LatLng(lat, lng));

            if (t < 1.0) 
            {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } 
            else 
            {
                if (hideMarker) 
                {
                    marker.setVisible(false);
                } 
                else 
                {
                    marker.setVisible(true);
                }
            }
        }
    });
}

谢谢!

【问题讨论】:

    标签: android google-maps google-maps-android-api-2


    【解决方案1】:

    您好,我的一个项目中也有同样的东西,它就像魅力一样。

    在此行之后添加以下代码:

    marker.setPosition(new LatLng(lat, lng));
    
     //this code
     if (googleMap != null)
             googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15.0f));
    

    希望对您有所帮助。 谢谢

    【讨论】:

      【解决方案2】:

      您必须在位置的新纬度和标记的当前纬度之间使用线性插值器或加速度插值器。这是一个使用反弹插值器的示例。

      final Handler handler = new Handler();
              final long start = SystemClock.uptimeMillis();
              final long duration = 2500;
      
              final Interpolator interpolator = new BounceInterpolator();
              marker.setVisible(true);
      
              handler.post(new Runnable() {
                  @Override
                  public void run() {
                      long elapsed = SystemClock.uptimeMillis() - start;
                      float t = Math.max(
                              1 - interpolator.getInterpolation((float) elapsed
                                      / duration), 0);
      
                      marker.setAnchor(0.5f, 1.0f + 6 * t);
                      marker.setPosition(latlng)
      
                      if (t > 0.0) {
                          // Post again 16ms later.
                          handler.postDelayed(this, 16);
                      }
                  }
              });
      

      【讨论】:

      • 感谢您的快速回复。让我做这个然后回来。令我惊讶的是,标记很难顺利移动。 :(
      • runnable 是制作简单动画的朋友。是的,它棘手不难。您可以轻松更改标记 latlng。 marker.setPosition(latlng) 但它也会有些笨拙。
      • 终于有机会做这个了。我用你的逻辑+其他参考想出了上面的代码。问题是使用它时我根本看不到我的标记。它出现然后消失。请看一下我的代码。让我知道问题可能出在哪里?谢谢!
      • 我认为它类似于 double lng = t * (toPosition.longitude - startpos.longitude) + startpos.longitude;如果您仔细考虑...插值从零开始并变为 1。因此,当您获得零时,公式变为 0*(toposition.longitude-startpos.longitude) + startpos.longitude = startpos.longitude 并且当您使用 a 进行插值时1 公式等于结束位置。 1*toposition.longitude - 1 * startpos.longitude + startpos.longitude = toposition.longitude。祝你好运。尝试使用更高级的 acceloratedecelorateinterpolator。祝你好运。
      • @TheDevMan 你找到解决方案了吗
      猜你喜欢
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多