简体   繁体   中英

Update marker position on google map smoothly

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.

This is code snippet.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM