如何顺利地在Google Maps v2 android中保持移动当前位置标记 [英] How to smoothly keep moving current location marker in Google Maps v2 android

查看:83
本文介绍了如何顺利地在Google Maps v2 android中保持移动当前位置标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的位置放了一个标记。我想平稳地移动我的标记,就像Google地图应用程序一样。当我继续在我的车中移动时,蓝色圆圈运动非常顺畅。我想为我的应用实现相同的功能。我如何在我的应用程序上实现这个功能?

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



以下是我所不知道的:

  googleMap.setMyLocationEnabled(真正); 

所以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(更新);
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);





$ b因此,每次调用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);
最终持续时间= 500;

final插值器插值器=新的LinearInterpolator();

handler.post(new Runnable()
{
@Override
public void run()
{
经过的时间= SystemClock。 uptimeMillis() - 开始;
float t = interpolator.getInterpolation((float)逝去的
/持续时间);
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)
{
// 16ms后再次发布
handler.postDelayed(this,16);
}
else
{
if(hideMarker)
{
marker.setVisible(false);
}
else
{
marker.setVisible(true);
}
}
}
});

谢谢!

解决方案

您必须在位置的新位置和标记的当前位置之间使用插值器,它可以是线性的,也可以是加速度插值器。这里有一个反射插值器的例子。

  final Handler handler = new Handler(); 
final long start = SystemClock.uptimeMillis();
最终持续时间= 2500;

最终插值器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.0
marker.setPosition(latlng)

if(t> 0.0){
// 16ms后再次发布
处理函数。 postDelayed(this,16);
}
}
});


I have placed a marker for my location. I would like to move my marker smoothly something like the Google maps app. The blue circle moves very smoothly when I keep moving in my car. I would like implement the same for my app. how do I implement this on my app?

As of now my location marker just jumps for different location on very location change and marks the marker there.

Here is what I have don:

 googleMap.setMyLocationEnabled(true);

So onMyLocationChange method is called automatically:

@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);
}

in mylocaiton method:

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);
}

So everytime mylocation is called the marker gets removed and calls mylocation method and then creates the marker in the new postition. Instead I would like to get a smooth transition of the marker like Google maps? How to implement this?

Update:

After working couple of times on this: I came to this code finally but then my markers are not showing up:

I am using the below method and calling this every time in myLocation method.

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);
                }
            }
        }
    });
}

Thanks!

解决方案

You'll have to use an interpolator a linear or maybe an acceleration interpolator between the new latlng of the location and the current latlng of the marker. Here's an example with a bounce interpolator.

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);
                }
            }
        });

这篇关于如何顺利地在Google Maps v2 android中保持移动当前位置标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆