如何在Android中的地图上的两个位置之间的路径上移动图像 [英] How to move an image over the path between two location on map in android

查看:104
本文介绍了如何在Android中的地图上的两个位置之间的路径上移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,其中显示了两个随机位置和它们之间的路径。我使用了这个教程可以完成。



现在,我想将移动图像从一个位置显示到另一个位置。我已经在这两个位置上放置了标记,并且我还将位置保存在了数组列表中。



这里是我移动drawable的代码:



'pre> mMap.setOnMarkerClickListener(新GoogleMap.OnMarkerClickListener(){
最终处理程序的处理程序=新的处理程序();
INT I = 0;

@覆盖
公共布尔onMarkerClick(标记标记){
//的System.out.println( 标记大小: - + MarkerPoints.size());
handler.post(新的Runnable(){

@覆盖
公共无效的run(){

BitmapDescriptor图标= BitmapDescriptorFactory.fromResource(R.drawable.truck_16) ;

而(I< MarkerPoints.size()){
的MarkerOptions的MarkerOptions =新的MarkerOptions()位置(MarkerPoints.get(I))
.title伪(当前位置)
图标(图标);

System.out.println(MarkerPoints.get(i));
mMap.addMarker(markerOptions);
i ++;
handler.postDelayed(this,3000);
}
}
});

返回true;
}
});


解决方案

最后,我找到了一种方法来完成这项工作。这不是最好的方法,但它解决了这个问题。如果有人需要做这样的事情,我将来会发布这样的内容,这可能会有帮助。



我有将所有点存储在一个名为 MarkerPoints 的ArrayList中,我们从API获取并使用它来绘制图像。



以下是代码:

  public class MyActivity extends FragmentActivity implements Runnable 
{
private线程线程= null;
volatile boolean isRunning;
private ArrayList< LatLng> MarkerPoints; //此数组列表包含路线上的所有点
int i = 0;
标记标记;


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_trip_maps);

isRunning = true;
}

@覆盖
公共无效的run(){

,而(isRunning){

runOnUiThread(新的Runnable (){
@Override
public void run(){
update();
}
});

control();



$ b public void update(){

if(marker!= null){
marker。去掉();
}
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.truck_16);
marker = mMap.addMarker(new MarkerOptions()。position(MarkerPoints.get(i))
.title(Current Location)
.icon(icon));

System.out.println(MarkerPoints.get(i));
i ++;
if(i> MarkerPoints.size() - 1){
isRunning = false;



$ b public void control(){
try {
thread.sleep(100);
} catch(InterruptedException e){
e.printStackTrace();
}
}


}



<这将给予移动的效果。


I am working on a project in which I am showing two random location and the path between them.I have used this tutorial to accomplish.

Now i want to show the moving image from one location to another.I have already put markers on that two locations.and also I have saved the position in an arraylist.

I had found some similar posts but couldn't solve my issue.

Here is my code for moving the drawable:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            final Handler handler = new Handler();
            int i = 0;

            @Override
            public boolean onMarkerClick(Marker marker) {
                // System.out.println("Marker size:- " + MarkerPoints.size());
                handler.post(new Runnable() {

                    @Override
                    public void run() {

                        BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.truck_16);

                        while (i < MarkerPoints.size()) {
                            MarkerOptions markerOptions = new MarkerOptions().position(MarkerPoints.get(i))
                                    .title("Current Location")
                                    icon(icon);

                            System.out.println(MarkerPoints.get(i));
                            mMap.addMarker(markerOptions);
                            i++;
                            handler.postDelayed(this, 3000);
                        }
                    }
                });

                return true;
            }
        });

解决方案

Finally I found a way to accomplish this.May be this is not the best way but it solves the problem.I am posting this so in future if someone needs to do stuff like that,It can be helpful.

I have store all the points in an ArrayList called MarkerPoints that we get from the API and use it to draw the image on ever point.

Here is the code:

public class MyActivity extends FragmentActivity implements Runnable
{
private Thread thread = null;
volatile boolean isRunning;
private ArrayList<LatLng> MarkerPoints; //This arrayList contains all points that are on the route
int i = 0;
Marker marker;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_on_trip_maps);

        isRunning = true;
    }

    @Override
    public void run() {

        while (isRunning) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    update();
                }
            });

            control();

        }
    }

    public void update() {

        if (marker != null) {
            marker.remove();
        }
        BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.truck_16);
        marker = mMap.addMarker(new MarkerOptions().position(MarkerPoints.get(i))
                .title("Current Location")
                .icon(icon));

        System.out.println(MarkerPoints.get(i));
        i++;
        if (i > MarkerPoints.size() - 1) {
            isRunning = false;

        }
    }

    public void control() {
        try {
            thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}

This will give the moving effect.

这篇关于如何在Android中的地图上的两个位置之间的路径上移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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