I\'m looking to do something like what is done right here in Android. What I need to do is group markers into different groups and have checkboxes to each group.
W
Refer this : https://stackoverflow.com/a/14508886/3134215
you can use marker.setVisible()
to show/hide markers.
You have to take array list of marker depend on category,
ArrayList<Marker> category1=new ArrayList<>();
ArrayList<Marker> category2=new ArrayList<>();
than add your maker on this array list, after remove on click event plz write this
for (int i=0; i< category1.size(); i++){
category1.get(i).setVisibility(false);
}
i think this is helpfull
There's a few hidden problems here. You need to separate out the different tasks:
You will need to keep a list of Restaurants and Bars, a SQLite database with a table for Restaurants and a table for Bars would be a good option.
You can query the database and populate a list of POJO objects, and use those lists to generate the Markers when the checkboxes are checked.
Then, you need to keep a separate list of the Markers, so that when the check-boxes are un-checked, you can remove the Markers from the map.
Here is a simple example that I put together quickly with hard coded values, you will need to add the database part later.
public class CheckMapActivity extends AppCompatActivity
implements OnMapReadyCallback {
GoogleMap googleMap;
List<MapLocation> restaurantList;
List<MapLocation> barList;
List<Marker> restaurantMarkers = new ArrayList<>();
List<Marker> barMarkers = new ArrayList<>();
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_map);
//Eventually load from a database
//Hard coded here for now
restaurantList = new ArrayList<>();
barList = new ArrayList<>();
MapLocation r1 = new MapLocation(37.78344,-122.42578, "Restaurant One" );
MapLocation r2 = new MapLocation(37.7876965,-122.4106738, "Restaurant Two" );
restaurantList.add(r1);
restaurantList.add(r2);
MapLocation b1 = new MapLocation(37.7866028,-122.4044511, "Bar One" );
MapLocation b2 = new MapLocation(37.7864459,-122.4090323, "Bar Two" );
barList.add(b1);
barList.add(b2);
CheckBox checkRestaurants = (CheckBox) findViewById(R.id.checkRestaurants);
checkRestaurants.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
showRestaurants();
} else {
hideRestaurants();
}
}
});
CheckBox checBars = (CheckBox) findViewById(R.id.checkBars);
checBars.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
showBars();
} else {
hideBars();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void setUpMap() {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
public void showRestaurants() {
restaurantMarkers.clear();
for (MapLocation loc : restaurantList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
restaurantMarkers.add(marker);
}
}
public void showBars() {
barMarkers.clear();
for (MapLocation loc : barList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
barMarkers.add(marker);
}
}
public void hideRestaurants(){
for (Marker marker : restaurantMarkers){
marker.remove();
}
}
public void hideBars(){
for (Marker marker : barMarkers){
marker.remove();
}
}
//POJO to hold locations:
public class MapLocation {
public MapLocation(double lt, double ln, String t){
lat = lt;
lon = ln;
title = t;
}
public double lat;
public double lon;
public String title;
}
}
Here's the layout, activity_check_map.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.daniel.tabstest.CheckMapActivity">
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="500dp"/>
<LinearLayout
android:layout_alignBottom="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restaurants" />
<CheckBox
android:id="@+id/checkBars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bars" />
</LinearLayout>
</RelativeLayout>
Result:
Restaurants checked:
Restaurants and Bars checked:
Bars checked:
Nothing checked: