1
  1. Iam getting Every Minute LatLong from Shared preferences, Showing on Map.

  2. Marker is Moving Perfectly in Every minute, but not removing the Old marker(Its Showing Every Minute New Marker.

  3. But closing and Opening the map fragment its showing Single Marker Perfectly.
  4. Please Help me how to fix this.also i tried Marker.Remove

  5. I called below method inside OnLocation Changed Method.

    /* Method to display the location on UI */

    private void displayLocation()
    {
    
    
        try
        {
    
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    
            if (mLastLocation != null)
            {
                double latitude = mLastLocation.getLatitude();
                double longitude = mLastLocation.getLongitude();
    
    
    
                // get user data from session
                HashMap<String, String> user = session.getGPSPING();
    
                // UserLat
                String  LatLongUser="";
                LatLongUser = user.get(SessionManagerFor_Register.KEY_LATLONG);
    
    
    
    
                if(!LatLongUser.equals(""))
                {
    
                    Log.i(" PING on MAP LatLong", LatLongUser);
    
                    String[] LanlongArr = LatLongUser.split("//");
                    List<String> Lanlonglist1 = Arrays.asList(LanlongArr);
    
                    int length = Lanlonglist1.size();
    
                    arraylist_DetailLineWalker = new ArrayList<String(length);
    
                    for (int i = 0; i < length; i++)
                    {
    
    
                        arraylist_DetailLineWalker.add(Lanlonglist1.get(i));
                    }
    
                    if(arraylist_DetailLineWalker!=null)
                    {
    
                        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_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("Walker Location");
    
                                markerOptionsLineWalker.icon(BitmapDescriptorFactory.fromResource(R.drawable.walker_outof_fence_icon_red));
    
    
                                Marker marker1 = googleMap.addMarker(markerOptionsLineWalker);
                                if (marker1 != null)
                                {
                                    marker1.remove();
                                }
                                //adding marker to the map
                                googleMap.addMarker(markerOptionsLineWalker);
                                // marker1.setPosition(latLng1);
                                Log.i(TAG, "Walker PING Added.............................");
    
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
    
                        }
    
                    }
                    else
                    {
                        Log.i("MAP NEwLatLong","TOTAL ARRY LIST NULLL");
                    }
    
    
                }
                else
                {
                    Log.i("MAP NEwLatLong","Null Not LatLong");
    
    
                }
    
    
            }
            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 e)
        {
            e.printStackTrace();
        }
    
    
    }
    

    }

Kumar
  • 969
  • 2
  • 22
  • 56

1 Answers1

3

Try this....

  Marker now;
  @Override
  public void onLocationChanged(Location location) {

  if(now != null){
        now.remove();   //if the marker is already added then remove it
    }

   // Getting latitude of the current location
   double latitude = location.getLatitude();

   // Getting longitude of the current location
    double longitude = location.getLongitude();

  // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);
    now = googleMap.addMarker(new MarkerOptions().position(latLng)));
 }

For Reference, visit this...

https://stackoverflow.com/a/16312869/6385873

Community
  • 1
  • 1
jaunvi
  • 111
  • 8
  • i already mentioned in my Question Marker.remove Not WOrking? – Kumar Jul 05 '16 at 06:06
  • You are adding marker 2 times, but removing only one time – jaunvi Jul 05 '16 at 06:19
  • Marker marker1 = googleMap.addMarker(markerOptionsLineWalker); /*first time marker added*/ if (marker1 != null) { marker1.remove(); } //adding marker to the map googleMap.addMarker(markerOptionsLineWalker); /*second time marker added*/ – jaunvi Jul 05 '16 at 06:22
  • @Kumar do you get it? – jaunvi Jul 05 '16 at 06:24
  • its removing all the Markers in For Loop. i don,t want to remove all. only Duplicated Marker I want to Remove. – Kumar Jul 05 '16 at 10:12