Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

1.In My app Gps LatLong is Getting from Server Every OneMinute. Saved in Shared Pref ,then getting the LatLong From shared pref Showing the Marker on the Map.

2.Every One Minute I want to Move the Marker based on the LatLong.

3.But While Changing the Marker Location. Getting Duplicates.

Please Help me to Solve this Issue.

Inside Oncreate method i Called below Snippet in Every 60 Secs for Calling a Method.

 try
        {
            Thread t = new Thread()
            {
                @Override
                public void run()
                {
                    try
                    {
                        while (!isInterrupted())
                        {
                            Thread.sleep(60*1000);
                            getActivity().runOnUiThread(new Runnable()
                            {
                                @Override
                                public void run()
                                {

                                    display_Location();
                                    Log.i("Every 60 Second","Current  Called..");

                                }
                            });
                        }
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            };
            t.start();

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

Method Iam USing:

private void display_Location()
    {

        try
        {

            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

            if (mLastLocation != null)
            {
        /*For Current Location ping Starts Here*/

                // get user data from session
                HashMap<String, String> user = session.getGPSPING();

                // UserLat
                String LatLongUser = "";
                LatLongUser = user.get(SessionManagerFor_Register.KEY_LATLONG);




                if (!LatLongUser.equals("") || LatLongUser != null)
                {

                    Log.i(" PING on MAP LatLong", LatLongUser);

                    String[] LanlongArr = LatLongUser.split("//");
                    List<String> Lanlonglist1 = Arrays.asList(LanlongArr);



                    int length = Lanlonglist1.size();

/*ArrayList For adding All ArrayList items in Single(Concating)*/


                    arraylist_DetailLineWalker = new ArrayList<String>(length);

                    for (int i = 0; i < length; i++) {


                        arraylist_DetailLineWalker.add(Lanlonglist1.get(i) 
                              );
                    }

                    if (arraylist_DetailLineWalker != null)
                    {

                        // Initializing
                        LineWalkermMarkers_arr = new ArrayList<Marker>();
                        //   just Remove Older Line Wlaker
                        if (LineWalkermMarkers_arr != null) {
                            // LineWalker_marker1.remove();
                            RemoveLineWalkerMarkers();

                            Log.i(TAG, "LineWalker REMOVED.............................");
                        }


                        for (int i = 0; i < arraylist_DetailLineWalker.size(); i++)
                        {
                            try 
                            {
                                String Val = arraylist_DetailLineWalker.get(i).toString();
                                //Log.i(" Validation Id",Val);
                                VALUE_ARRAY_STRING = Val.toString().split("::");

                                LatLong_DataSaveTable = VALUE_ARRAY_STRING[0].toString();



                                System.out.println("checking STarted LatLong::" + LatLong_DataSaveTable);

                                String[] latlong = LatLong_DataSaveTable.split(",");
                                double latitude1 = Double.parseDouble(latlong[0]);
                                double longitude2 = Double.parseDouble(latlong[1]);

                                //To hold location
                                LatLng latLng1 = new LatLng(latitude1, longitude2);
                                //To create marker in map
                                MarkerOptions markerOptionsLineWalker = new MarkerOptions();
                                markerOptionsLineWalker.position(latLng1); //setting position
                                markerOptionsLineWalker.draggable(true); //Making the marker draggable
                                markerOptionsLineWalker.title("USER LOCAITON");




                                    markerOptionsLineWalker.icon(BitmapDescriptorFactory.fromResource(R.drawable.walker_outof_fence_icon_red));



                                //adding marker to the map
                                // googleMap.addMarker(markerOptionsLineWalker);
                                LineWalker_marker1 = googleMap.addMarker(markerOptionsLineWalker);

                                LineWalkermMarkers_arr.add(LineWalker_marker1);
                                // LineWalker_marker1.setPosition(latLng1);
                                Log.i(TAG, " NEW Line Walkers PING Added.............................");

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

                        }

                    } else {
                        Log.i("MAP NEwLatLong", "TOTAL ARRY LIST NULLL");
                    }


                }
                else
                {
                    Log.i("MAP NEwLatLong", "Null Not LatLong");

                    Toast.makeText(getActivity(), "Lat Long Not Available..!", Toast.LENGTH_SHORT).show();
                }


            }
            else
            {

                Log.i("Location EXception", "Couldn't get the location. Make sure location is enabled on the device");
                // can't get location
                // GPS or Network is not enabled
                // Ask user to enable GPS/network in settings
                gps.showSettingsAlert();
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }


    /*Remove the Linewalker*/
    private void RemoveLineWalkerMarkers()
    {
        for (Marker marker: LineWalkermMarkers_arr)
        {
            marker.remove();
        }
        LineWalkermMarkers_arr.clear();
    }
like image 434
Kumar Avatar asked Mar 11 '23 18:03

Kumar


2 Answers

if(arraylist_DetailLineWalker != null && arraylist_DetailLineWalker.size()>0){
    arraylist_DetailLineWalker.clear()
    mMap.clear();
    showMarker();
}
like image 198
Akshay GS Avatar answered Apr 24 '23 08:04

Akshay GS


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

like image 33
antonio Avatar answered Apr 24 '23 07:04

antonio