Move marker with gps in google map android

前端 未结 8 790
悲&欢浪女
悲&欢浪女 2020-12-15 19:31

I want to make move of the marker in GOOGLE MAP while gps location changes just like in UBER app. I have found some solutions but unab

相关标签:
8条回答
  • 2020-12-15 20:29

    This can be done using CameraPosition, googleMap.animateCamera and marker movement animation using linear interpolator.

    You can take a look at this tutorial here and the respective github page.

    This tutorial uses google maps v2. Hope this helps.

    0 讨论(0)
  • 2020-12-15 20:35

    Inorder to animate just call this method (animateMarker) with previous location and new location along with Marker object

    private Marker mCurrentMarker;
    private float ZOOMLEVEL=18.0f;
    private LatLng previousLatLon;
    private Handler mLocalHandler;
    private GoogleMap mGoogleMap;
    
    public void animateMarker(final Marker marker, final LatLng toPosition,final LatLng fromPosition) {
    
    
            final long duration = 500;
            final Interpolator interpolator = new LinearInterpolator();
    
            mLocalHandler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - mStartTime;
                    float t = interpolator.getInterpolation((float) elapsed
                            / duration);
                    marker.setPosition(toPosition);
                    marker.setAnchor(Constants.MAPANCHOR, Constants.MAPANCHOR);
                    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, ZOOMLEVEL));
                    if (t < 1.0) {
                        // Post again 16ms later.
                        mLocalHandler.postDelayed(this, 16);
                    } else {
                        marker.setVisible(true);
                    }
                    }
                }
            });
            previousLatLon=toPosition;// reassign the previous location to current location
        }
    
    0 讨论(0)
提交回复
热议问题