顺利更新Google地图上的标记位置 [英] Update marker position on google map smoothly

查看:104
本文介绍了顺利更新Google地图上的标记位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在android应用中开发订单跟踪模块,在此模块中,我实现了一个源位置标记和目标位置标记,源位置是固定的,并且只要更改位置,目标位置标记就会不断变化;但是,当位置更改时,标记会从一个位置跳到另一个位置,因此移动并不平稳, 实际上,我正在删除上一个标记,并在新位置创建一个新标记, 有什么方法可以平滑地移动标记,还是可以更新先前的标记位置?请帮我.

I am developing order tracking module in android app, in this module, I implemented one source location marker and destination location marker, source location is fixed, and destination location marker is keep on changing, whenever the location is changed,But when location was changed, the marker is jumping from one position to another, it is not moving smoothly, Actually I am deleting the prev marker and creating new one at new position, is there any way to move marker smoothly or can i update previous marker position ? please help me.

这是代码段.

public  void  updatePackageLoc(){
        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {

                while(updatelocation) {
                    Track_Location();
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (package_marker != null && package_newlat != 0.0 && package_newlon != 0.0) {
                                package_marker.remove();
                                LatLng position = new LatLng(package_newlat, package_newlon);
                                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,14f));
                                package_marker = googleMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromResource(R.drawable.pacage)));
                            }

                        }
                    });

                }
            }
        };
        new Thread(runnable).start();
}

推荐答案

这对我有用,将finalPosition声明为全局变量,并在Track_location函数中进行了更新.

This worked for me, Declared finalPosition as global variable and updated in Track_location function.

public  void  updatePackageLoc(){
            final Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {

                    while(updatelocation) {
                        Track_Location();
                        try {
                            Thread.sleep(3000);

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        handler.post(new Runnable() {
                            long elapsed;
                            float t;
                            float v;
                            @Override
                            public void run() {
                                // Calculate progress using interpolator
                                elapsed = SystemClock.uptimeMillis() - start;
                                t = elapsed / durationInMs;
                                v = interpolator.getInterpolation(t);
                                //  Log.d("order_track",startPosition.toString()+ " " +finalPosition.toString());
                                LatLng currentPosition = new LatLng(
                                        startPosition.latitude*(1-t)+finalPosition.latitude*t,
                                        startPosition.longitude*(1-t)+finalPosition.longitude*t);

                                package_marker.setPosition(currentPosition);
                                // Repeat till progress is complete.
                                if (t < 1) {
                                    // Post again 16ms later.
                                    handler.postDelayed(this, 16);
                                }
                            }
                        });


                    }
                }
            };
            new Thread(runnable).start();
        }

这篇关于顺利更新Google地图上的标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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