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

I would like (in my Android application to use Google maps api v2) to hide or show markers on my GoogleMap object according to the category, such as on the google maps api web page, for example:

I have a GoogleMap with 50 markers, 20 of them are restaurants, 20 of them are bus stops, and 10 are movie theaters.

Is it possible on Android google maps api v2 to filter on these markers by hiding all restaurant markers if we uncheck, for example?

I would like to do something similar, 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'm new.

+7
source share
3 answers

Try this way.

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

Click event

  restuarantMarkers.setVisible(false); 

This method may be performed using a loop.

Let me know if this works for you.

+20
source

You can use the 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(); 
+2
source

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

// mMap is an instance of GoogleMap that has already been initialized by 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 identifier, 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()); } } } } } 
0
source

All Articles