Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android marker remove() method not working

I use List to store marker to add to gmap and remove component it self.

List<Location> listLocation = new ArrayList<Location>();

When location updated. I were store Location to listLocation,remove the old marker . Then add the newest location to Gmap.

     @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            countUpdatePos++;

        listLocation.add(location);
        LatLng lastLocation = new LatLng(location.getLatitude(),
                location.getLongitude());
        gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastLocation, 16));
        String cityName = null;
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses = null;
        try {
            addresses = gcd.getFromLocation(location.getLatitude(),
                    location.getLongitude(), 1);
            if (addresses.size() > 0) {
                int a = addresses.get(0).getMaxAddressLineIndex();
                for (int i = 0; i < a; i++) {
                    if (i == 0) {
                        cityName = addresses.get(0).getAddressLine(i);
                    } else {
                        cityName = cityName + ", "
                                + addresses.get(0).getAddressLine(i);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (countUpdatePos == 1) {
            Toast.makeText(getApplicationContext(),
                    getResources().getString(R.string.updated_position), 2000)
                    .show();
            progressToLoadMap.setVisibility(View.GONE);
            checkStartActivity = true;
            timer = new MyCount((int) totalTime * 60 * 1000, 1000);
            timer.start();
        }

        if (listLocation.size() > 1) {
            listLocation.add(location);
//          gmap.clear();
            LatLng newLocation = new LatLng(location.getLatitude(),
                    location.getLongitude());
            gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLocation, 16));

            myMarker = gmap.addMarker(new MarkerOptions().position(new LatLng(
                    listLocation.get(listLocation.size() - 1).getLatitude(),
                    listLocation.get(listLocation.size() - 1).getLongitude())));
            if (myMarker != null) {
                myMarker.remove();
                myMarker = null;
            }
            gmap.addMarker(new MarkerOptions()
                    .title(getResources().getString(
                            R.string.current_location_found)).snippet(cityName)
                    .position(newLocation));
            if (listLocation.get(listLocation.size() - 1) != null
                    && listLocation.get(listLocation.size() - 1) != null) {
                gmap.addPolyline(new PolylineOptions()
                        .add(new LatLng(listLocation.get(
                                listLocation.size() - 1).getLatitude(),
                                listLocation.get(listLocation.size() - 1)
                                        .getLongitude()),
                                new LatLng(location.getLatitude(), location
                                        .getLongitude())).width(3)
                        .color(Color.BLUE));
            }

But when run activity, all marker still display, and no polyline add to gmap :(. Help me here is picture enter image description here

like image 987
kemdo Avatar asked Sep 02 '25 03:09

kemdo


2 Answers

I hade the same problem and after checking my code I found that I am calling the method that add the marker twice in the oncreate and onresume events so 2 markers are added at the same position on the map so when I remove the marker using marker.remove() method one is removed but the other one remain so I thought it is not deleted. I removed the method call from the oncreate event and now it is working fine.

like image 157
A.Awada Avatar answered Sep 04 '25 19:09

A.Awada


What you have done is correct but Just need little bit modification for Marker: Set this globally:

Marker myMarker;

In you onCreate()

myMarker = gmap.addMarker(new MarkerOptions()
                    .position(new LatLng(listLocation.get(
                            listLocation.size() - 1).getLatitude(),
                            listLocation.get(listLocation.size() - 1)
                                    .getLongitude())));

Then Whenver you want to add another Marker then Just check it out if Is there already any other marker is present. If yes then remove it if you want.

For that:

if(myMarker!=null)
{
  marker.remove();
  marker = null;
}

myMarker  = gmap.addMarker(new MarkerOptions()
                .title(getResources().getString(
                        R.string.current_location_found)).snippet(cityName)
                .position(newLocation));

For Polyline you might check if lat,long are not null.

like image 40
Sagar Shah Avatar answered Sep 04 '25 18:09

Sagar Shah