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

大憨熊 提交于 2019-11-29 12:33:54

问题


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.


回答1:


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.




回答2:


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();



回答3:


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());

            }

        }

    }

    }

   }



回答4:


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));

}


来源:https://stackoverflow.com/questions/14507821/is-it-possible-to-show-hide-markers-in-android-google-maps-api-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!