17

I would like (in my Android app using Google maps api v2) to hide or show markers on my GoogleMap object according to a category, just like in the google maps web api, for example:

I have a GoogleMap with 50 Markers, 20 of them represent restaurants, 20 them represent bus stops, and 10 are cinemas.

Is it possible on Android google maps api v2 to do filtering on these markers, by hiding all the restaurant markers if we un-tick a checkbox for example?

I would like to do something like that but on my Android device using google maps api v2: http://www.geocodezip.com/v3_MW_example_categories.html

Sorry for the basic question but I am a beginner.

Rami
  • 8,044
  • 18
  • 66
  • 108
  • yes its possible to hide some markers – moDev Jan 24 '13 at 19:04
  • Hi Mitesh, I will try it on Monday and tell you if it works, thank you :) – Rami Jan 26 '13 at 23:27
  • 1
    I was hoping there would be a way to add points from different categories to different Overlays and then showing/hiding overlays accordingly. No such Overlay concept in app v2? – vincebodi Apr 17 '15 at 16:23

4 Answers4

29

Try this way.

 Marker restuarantMarkers = gMap.addMarker(new MarkerOptions()
                .position(latlng)
                .title("MyPlace").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin)).draggable(true));

On Click Event

  restuarantMarkers.setVisible(false);

This way can do using loop..

Let me know if it works for you.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
moDev
  • 5,248
  • 4
  • 33
  • 63
  • Thank you for the hint Mitesh, I managed to do it based on your answer. thank you :) – Rami Jan 28 '13 at 17:23
  • @droid_dev: is it possible to store marker reference in arraylist or hashmap and then show hide marker by arry or hasmap. Means show all markers of contained in one hashmap – Wikki Aug 25 '14 at 00:25
  • @Wikki Yes its possible – moDev Aug 25 '14 at 10:48
  • @droid_dev : I'm looking for a tutorial on show / hide the specific markers in google maps v2 using hashmap? where I get the link..thanks – ltvie Nov 03 '14 at 03:38
  • What does this even do? – Neo42 Aug 15 '18 at 21:50
3

You can use dialog if you want to filter your locations.

final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.dialog);
        Button bt_ok = (Button) dialog.findViewById(R.id.button1);
        final CheckBox cb1 = (CheckBox) dialog.findViewById(R.id.checkBox1);
        final CheckBox cb2 = (CheckBox) dialog.findViewById(R.id.checkBox2);
        dialog.setTitle("Category");

        bt_ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mMap.clear();
                if (cb1.isChecked()){
                    mMap.addMarker(new MarkerOptions().position(new LatLng(44.109798, 15.242270)).title("Pin").icon(BitmapDescriptorFactory.fromResource(R.drawable.museum)));
                }
                if (cb2.isChecked()){
                    mMap.addMarker(new MarkerOptions().position(new LatLng(44.209798, 15.392270)).title("Pin 2").icon(BitmapDescriptorFactory.fromResource(R.drawable.restaurants)));
                }
                dialog.dismiss();
            }

        });

        dialog.show();
Kristijan Drača
  • 614
  • 4
  • 16
0

Yes you can! And here's how...

//mMap is an instance of GoogleMap that has already been initialized else where

mMap.setOnCameraChangeListener(getCameraChangeListener());
getCameraChangeListener() 
public OnCameraChangeListener getCameraChangeListener()

{
return new OnCameraChangeListener()
{
    @Override
    public void onCameraChange(CameraPosition position)

    {
        addItemsToMap(this.items);
    }

};

}

//Your "Item" class will need at least a unique id, latitude and longitude.

private void addItemsToMap(List<Item> items)
{
  if(this.mMap != null)

{

    LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;

    for(Item item : items)
    {

        if(bounds.contains(new LatLng(item.getLatitude(), item.getLongitude()))

        {
            if(!courseMarkers.containsKey(item.getId()))

            {

                this.courseMarkers.put(item.getId(),     this.mMap.addMarker(getMarkerForItem(item)));

            }

        }
        else
        {

            if(courseMarkers.containsKey(item.getId()))

            {

             courseMarkers.get(item.getId()).remove();
             courseMarkers.remove(item.getId());

            }

        }

    }

    }

   }
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • i'm not sure about the check box thing but this is code to add and remove markers – Rachel Gallen Jan 24 '13 at 18:30
  • You would have to find the latitude and longitude using geocoding. There is a Geocoder class in Android. – Rachel Gallen Jan 24 '13 at 18:31
  • Hi Rachel, the code is bout adding or deleting markers according to the latitude and longitude, I do not need this. I would like to hide or show according a category or description or title or anything like this. – Rami Jan 24 '13 at 18:32
  • see http://stackoverflow.com/questions/12905930/grouping-markers-by-category-to-show-and-hide – Rachel Gallen Jan 24 '13 at 18:37
  • 1
    Thank you again Rachel but the links that you posted is not for Android, is for a web application :) I edited my question by putting your second link. I would like to do that using Android google maps not in a web page :) – Rami Jan 24 '13 at 18:39
  • could you convert it somehow? – Rachel Gallen Jan 24 '13 at 18:40
  • There is no direct conversion, that is why I am asking. – Rami Jan 24 '13 at 18:40
  • see this link it may help you can set visible to false https://developers.google.com/maps/documentation/android/marker – Rachel Gallen Jan 24 '13 at 19:03
0

Here, LocationArray is ArrayList of Locations, which are plotted on map. We want to show all markers plotted on map.

 private void FitAllMarkers() {


    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (int i = 0; i < LocationArray.size(); i++) {
        builder.include(new LatLng(Double.parseDouble(LocationArray.get(i).getLat()), Double.parseDouble(LocationArray.get(i).getLng())));

    }


    LatLngBounds bounds = builder.build();
    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));

}
Pratibha Sarode
  • 1,819
  • 17
  • 17