Removing markers in android

I have a Global array object, for example Marker marker_array[]; and then in the layout, click I, initializing it as marker_array = new Marker[8]; . I want to add markers to display on this layout and remove the second click so that I create a clickcount Global variable with a null value.

My correct code is here

 final RelativeLayout layout = (RelativeLayout) findViewById(R.id.track_div); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clickcount++; point_new = new LatLng[8]; point_new[0] = new LatLng(31.5301843, 74.3207487); point_new[1] = new LatLng(31.5214693,74.3236027); point_new[2] = new LatLng(31.5194393, 74.3257327); point_new[3] = new LatLng(31.4942166, 74.3004533); point_new[4] = new LatLng(31.4864646, 74.2911203); point_new[5] = new LatLng(31.4803596, 74.2787933); point_new[6] = new LatLng(31.4764716, 74.2638203); point_new[7] = new LatLng(31.4775236, 74.2628873); // initialize marker_array; marker_array = new Marker[8]; Toast.makeText(getApplicationContext(), "count "+clickcount, Toast.LENGTH_SHORT).show(); // if (clickcount % 2 == 0) { polyline.setVisible(false); for (int i = 0; i < point_new.length; i++){ Toast.makeText(getApplicationContext(), "marker length ="+marker_array.length, Toast.LENGTH_SHORT).show(); marker_array[i].remove(); // marker_array.setVisible(false); } } else { polyline.setVisible(true); for (int i = 0; i < point_new.length; i++) { // marker_array = new Marker[point_new.length]; MarkerOptions markerOptions = new MarkerOptions() .position(point_new[i]); marker_array[i] = mMap.addMarker(markerOptions); marker_array[i].setTitle("Points"); marker_array[i].setSnippet("Distance = 9.6 km, Time = 20 minute/s"); marker_array[i].setIcon(BitmapDescriptorFactory.fromResource(R.drawable.bus)); } } 

The problem is that it creates all 8 markers. But does not delete. Even if in the case when I try to remove the markers, Toast shows the correct length of 8 . Butt , when I delete any of marker_array separately as marker_array[7] , it deletes it.

How to remove all markers in marker_array without the map.clear(); method map.clear(); because I have other things, such as a polyline, etc. that I do not want to delete.

Any efforts would be appreciated.

+3
android google-maps google-maps-markers
Dec 11 '15 at 7:17
source share
3 answers

Use this to add markers.

How global

 List<Marker> mMarkers = new ArrayList<Marker>(); 

And in your for loop add markers to this list, e.g.

 for (int i = 0; i < point_new.length; i++) { MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(point_new[i]); Marker marker = mMap.addMarker(markerOptions); marker.setTitle("Point"); marker.setSnippet("this is snippet"); marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.p)); mMarkers.add(marker); // <-- Like this } 

And to remove markers

 private void removeMarkers() { for (Marker marker: mMarkers) { marker.remove(); } mMarkers.clear(); } 

hope this helps.

+5
Dec 11 '15 at 11:10
source share

Try it,

 private ArrayList<Marker> mMarkers; ... private void removeMarkers() { for (Marker marker: mMarkers) { marker.remove(); } mMarkers.clear(); 

}

+2
Dec 11 '15 at 7:24
source share

Method signature for addMarker :

 public final Marker addMarker (MarkerOptions options) 

Therefore, when you add a marker to GoogleMap by specifying marker parameters, you must save the returned Marker object (instead of the MarkerOptions object that you used to create it). This object allows you to subsequently change the state of the marker. When you are done with the marker, you can call Marker.remove() to remove it from the map.

As an aside, if you want to temporarily hide it, you can switch the marker visibility by calling Marker.setVisible(boolean) .

Similarly Remove Marker from GoogleMap

+1
Dec 11 '15 at 9:23
source share



All Articles