Every 1 Minute Gps LatLong Getting From Server Showing In Map as Marker. But Marker Geting Duplicating

岁酱吖の 提交于 2019-12-05 17:04:13

You are calling RemoveLineWalkerMarkers() after initializing LineWalkermMarkers_arr doing LineWalkermMarkers_arr = new ArrayList<Marker>();, so you are never removing your markers.

Just initialize your LineWalkermMarkers_arr after removing the markers:

if (LineWalkermMarkers_arr != null) {
    RemoveLineWalkerMarkers();
    Log.i(TAG, "LineWalker REMOVED.............................");
}
LineWalkermMarkers_arr = new ArrayList<Marker>();

As a side note, you should follow the Java code conventions (variables and method names should start with lowercase). You can find good guides here and here

if(arraylist_DetailLineWalker != null && arraylist_DetailLineWalker.size()>0){
    arraylist_DetailLineWalker.clear()
    mMap.clear();
    showMarker();
}

Solution is just a logic change

  1. Initialize Marker only once , either onCreate or some other method according to your logic
  2. If the markers are multiple, then re-initialization should be done once data is received.
  3. Created markers can be cleared with below logic

if(mGoogleMap != null) {
        mGoogleMap.clear();
 }
  1. Either reuse this marker to move from last location to new location. Or recreate all the markers, once data is received

//With your logic , this check should be done
if(arraylist_DetailLineWalker.size()>0){
   RemoveLineWalkerMarkers();
}
LineWalkermMarkers_arr = new ArrayList<Marker>();
for (int i = 0; i < arraylist_DetailLineWalker.size(); i++)
{
}

Alternative easy method to move single marker , to show live driving direction kind of feature

    private Marker mCurrentMarker;
    private float ZOOMLEVEL=18.0f;
    private LatLng previousLatLon;
    private Handler mLocalHandler;
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLocalHandler = new Handler();
        previousLatLon=new LatLng(45.320372, 2.460161);
        //Initialize Marker once Google Map object is created
        mMarkerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker_icon));
        mMarkerOptions.position(previousLatLon);
        mCurrentMarker = mGoogleMap.addMarker(mMarkerOptions);


    }

    /**
     * Call this method to move marker in map to new location in map with updated location
     * @param marker
     * @param toPosition
     * @param fromPosition
     */
    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
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!