Is it possible to show/hide Markers in Android Google maps api v2?
Asked Answered
A

4

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.

Albata answered 24/1, 2013 at 18:4 Comment(3)
yes its possible to hide some markersAvuncular
Hi Mitesh, I will try it on Monday and tell you if it works, thank you :)Albata
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?Appeasement
A
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.

Avuncular answered 24/1, 2013 at 19:10 Comment(5)
Thank you for the hint Mitesh, I managed to do it based on your answer. thank you :)Albata
@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 hashmapBioscope
@Bioscope Yes its possibleAvuncular
@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..thanksPredictory
What does this even do?Brandebrandea
M
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();
Miocene answered 21/3, 2013 at 11:22 Comment(0)
M
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());

            }

        }

    }

    }

   }
Mot answered 24/1, 2013 at 18:28 Comment(8)
i'm not sure about the check box thing but this is code to add and remove markersMot
You would have to find the latitude and longitude using geocoding. There is a Geocoder class in Android.Mot
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.Albata
see #12906430Mot
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 :)Albata
could you convert it somehow?Mot
There is no direct conversion, that is why I am asking.Albata
see this link it may help you can set visible to false developers.google.com/maps/documentation/android/markerMot
S
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));

}
Shipshape answered 16/7, 2018 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.