Android, How to remove all markers from Google Map V2?

后端 未结 5 1311
刺人心
刺人心 2020-12-25 10:04

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.

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 11:00

    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 markerList = new ArrayList(); 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 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

提交回复
热议问题