Android, How to remove all markers from Google Map

2020-02-18 04:20发布

问题:

I have map view in my fragment. I need to refresh map and add different markers based on condition. So, I should remove last markers from map before add new markers.

Actually, some weeks ago app was working fine and suddenly it happened. My code is like this:

private void displayData(final List<Venue> venueList) {

        // Removes all markers, overlays, and polylines from the map.
        googleMap.clear();
.
.
.
}

Last time it was working fine (before new Google Map API announce by Android team in I/O 2013). However, after that I adapted my code to use this new API. Now, I don't know why this method googleMap.clear(); doesn't work!

Any suggestion would be appreciated. Thanks

=======

Update

=======

Complete code:

private void displayData(final List<Venue> venueList) {

        // Removes all markers, overlays, and polylines from the map.
        googleMap.clear();

        // Zoom in, animating the camera.
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_LEVEL), 2000, null);

        // Add marker of user's position
        MarkerOptions userIndicator = new MarkerOptions()
                .position(new LatLng(lat, lng))
                .title("You are here")
                .snippet("lat:" + lat + ", lng:" + lng);
        googleMap.addMarker(userIndicator);

        // Add marker of venue if there is any
        if(venueList != null) {
            for(int i=0; i < venueList.size(); i++) {
                Venue venue = venueList.get(i);
                String guys = venue.getMaleCount();
                String girls= venue.getFemaleCount();
                String checkinStatus = venue.getCan_checkin();
                if(checkinStatus.equalsIgnoreCase("true"))
                    checkinStatus = "Checked In - ";
                else
                    checkinStatus = "";

                MarkerOptions markerOptions = new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude())))
                        .title(venue.getName())
                        .snippet(checkinStatus + "Guys:" + guys + " and Girls:" + girls)
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_orange_pin));

                googleMap.addMarker(markerOptions);
            }
        }

        // Move the camera instantly to where lat and lng shows.
        if(lat != 0  && lng != 0)
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), ZOOM_LEVEL));

        googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;
            }
        });

        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                String str = marker.getId();
                Log.i(TAG, "Marker id: " + str);
                str = str.substring(1);
                int markerId = Integer.parseInt(str);
                markerId -= 1; // Because first item id of marker is 1 while list starts at 0
                Log.i(TAG, "Marker id " + markerId + " clicked.");

                // Ignore if User's marker clicked
                if(markerId < 0)
                    return;

                try {
                    Venue venue = venueList.get(markerId);
                    if(venue.getCan_checkin().equalsIgnoreCase("true")) {
                        Fragment fragment = VenueFragment.newInstance(venue);
                        if(fragment != null)
                            changeFragmentLister.OnReplaceFragment(fragment);
                        else
                            Log.e(TAG, "Error! venue shouldn't be null");
                    }
                } catch(NumberFormatException e) {
                    e.printStackTrace();
                } catch(IndexOutOfBoundsException e) {
                    e.printStackTrace();
                }
            }
        });

回答1:

Okay finally I found a replacement way to solve my problem. The interesting problem is when you assign a marker to map, it's id is 'm0'. When you remove it from map and assign new marker you expect the id should be 'm0' but it's 'm1'. Therefore, it showed me the id is not trustable. So I defined List<Marker> markerList = new ArrayList<Marker>(); somewhere in onActivityCreated() of my fragment.

Then changed above code with following one. hope it helps others if they have similar issue with markers.

private void displayData(final List<Venue> venueList) {
        Marker marker;

        // Removes all markers, overlays, and polylines from the map.
        googleMap.clear();
        markerList.clear();

        // Zoom in, animating the camera.
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_LEVEL), 2000, null);

        // Add marker of user's position
        MarkerOptions userIndicator = new MarkerOptions()
                .position(new LatLng(lat, lng))
                .title("You are here")
                .snippet("lat:" + lat + ", lng:" + lng);
        marker = googleMap.addMarker(userIndicator);
//        Log.e(TAG, "Marker id '" + marker.getId() + "' added to list.");
        markerList.add(marker);

        // Add marker of venue if there is any
        if(venueList != null) {
            for (Venue venue : venueList) {
                String guys = venue.getMaleCount();
                String girls = venue.getFemaleCount();
                String checkinStatus = venue.getCan_checkin();
                if (checkinStatus.equalsIgnoreCase("true"))
                    checkinStatus = "Checked In - ";
                else
                    checkinStatus = "";

                MarkerOptions markerOptions = new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude())))
                        .title(venue.getName())
                        .snippet(checkinStatus + "Guys:" + guys + " and Girls:" + girls)
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_orange_pin));

                marker = googleMap.addMarker(markerOptions);
//                Log.e(TAG, "Marker id '" + marker.getId() + "' added to list.");
                markerList.add(marker);
            }
        }

        // Move the camera instantly to where lat and lng shows.
        if(lat != 0  && lng != 0)
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), ZOOM_LEVEL));

        googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;
            }
        });

        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                int markerId = -1;

                String str = marker.getId();
                Log.i(TAG, "Marker id: " + str);
                for(int i=0; i<markerList.size(); i++) {
                    markerId = i;
                    Marker m = markerList.get(i);
                    if(m.getId().equals(marker.getId()))
                        break;
                }

                markerId -= 1; // Because first item of markerList is user's marker
                Log.i(TAG, "Marker id " + markerId + " clicked.");

                // Ignore if User's marker clicked
                if(markerId < 0)
                    return;

                try {
                    Venue venue = venueList.get(markerId);
                    if(venue.getCan_checkin().equalsIgnoreCase("true")) {
                        Fragment fragment = VenueFragment.newInstance(venue);
                        if(fragment != null)
                            changeFragmentLister.OnReplaceFragment(fragment);
                        else
                            Log.e(TAG, "Error! venue shouldn't be null");
                    }
                } catch(NumberFormatException e) {
                    e.printStackTrace();
                } catch(IndexOutOfBoundsException e) {
                    e.printStackTrace();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });
    }


回答2:

If you want to clear "all markers, overlays, and polylines from the map", use clear() on your GoogleMap.



回答3:

Use map.clear() to remove all markers from Google map



回答4:

Suppose there is an ArrayList of 2 locations. Now, you display markers on the map based on that array. There will be two markers. When you click on the first marker it gives you a marker index m0 and the second is m1.

Say that you refresh location array and now you got an array with 3 locations. You got 3 markers. But when you click on the first one, it gives you marker index m2 (as if it continues counting from the first location arraw) the second is m3 and the third is m4. What you actually want is to make it as m0, m1, m2.

Now, when you build you location array you probably call location.add("you location")... and when you rebuild it (refresh it) you call location.clear() first and then build it again.

SOLUTION:

First, make another dummy array similar to location array and build it in for loop together with a real location array: locaionDummy.add(i) but don't you EVER refresh it - that way it keeps building and you will know how many locations you've ever had from the very beginning.

Second, do something like this (example of setting image) with mIndex as int variable:

void locatePins() {  

mIndex = locationDummy.size()-location.size();

for (int i = 0; i < userID.size(); i++) {

    LatLng pgLocation = new LatLng(Double.parseDouble(latArr.get(i)), Double.parseDouble(lngArr.get(i)));

    myMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {    
        @Override
        public View getInfoWindow(Marker marker) {

        View view = getLayoutInflater().inflate(R.layout.map_marker_info, null);

        RelativeLayout markerInfo= view.findViewById(R.id.markerInfo);
        TextView name = view.findViewById(R.id.userName);
        TextView details = view.findViewById(R.id.userInfo);
        ImageView img = view.findViewById(R.id.userImg);

        name.setText(marker.getTitle());
        details.setText(marker.getSnippet());

        img.setImageBitmap (bmImg.get(Integer.parseInt(marker.getId().replaceAll("[^\\d.]", ""))-mIndex));

        return view;
    }

    @Override
    public View getInfoContents(Marker marker) {

         return null;
    }   

    // ... the rest of the code 
}
}

The key is to subtract the real location.size() from a locationDummy.size() to get a number int mIndex that you will subtract later on from marker.getId()