¿Cómo ocultar / mostrar grupos de marcadores por categoría con Google Maps en Android?
Estoy buscando hacer algo como lo que está hechoaquí mismo en Android Lo que necesito hacer es agrupar marcadores en diferentes grupos y tener casillas de verificación para cada grupo.
Cuando marque y desmarque las casillas de verificación, los marcadores deben mostrarse y ocultarse.
Aquí está mi archivo MapsActivity.java.
public class MapsActivity extends FragmentActivity {
//Restaurants
private final LatLng LOCATION_DINEMORE = new LatLng(6.9270786,79.861243);
private final LatLng LOCATION_BARISTA = new LatLng(6.0334009,80.218384);
//Hotels
private final LatLng LOCATION_AVENRA = new LatLng(7.211111,79.838610);
private final LatLng LOCATION_MOUNTBATTEN = new LatLng(7.296411,80.635012);
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
// Give text to buttons
Button buttonloc1 = (Button)findViewById(R.id.btnLoc1);
buttonloc1.setText("Dinemore");
Button buttonloc2 = (Button)findViewById(R.id.btnLoc2);
buttonloc2.setText("Barista");
Button buttoncity = (Button)findViewById(R.id.btnCity);
buttoncity.setText("My Location");
Button buttonremove = (Button)findViewById(R.id.removeMarker);
buttonremove.setText("Remove");
/* Location myLocation = mMap.getMyLocation();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()))
.title("Hi I'm Here..:D")
);*/
// Marker to Dinemore
mMap.addMarker(new MarkerOptions()
.position(LOCATION_DINEMORE)
.title("Dinemore")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
);
// Marker to Barista
mMap.addMarker(new MarkerOptions()
.position(LOCATION_BARISTA)
.title("Barista Lavazza")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
);
// Marker to Avenra Garden Hotel
mMap.addMarker(new MarkerOptions()
.position(LOCATION_AVENRA)
.title("Avenra Garden")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
);
// Marker to Mounbatten Bunglow
mMap.addMarker(new MarkerOptions()
.position(LOCATION_MOUNTBATTEN)
.title("Mounbatten Bunglow")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
);
//When touch again on the map marker title will hide
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
}
});
ArrayList<Marker> category1=new ArrayList<>();
ArrayList<Marker> category2=new ArrayList<>();
for (int i=0; i< category1.size(); i++){
category1.get(i).remove();
}
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
public void onClick_City(View v){
mMap.setMyLocationEnabled(true);
}
// When click on this button, Map shows the place of Dinemore
public void onClick_Loc1(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_DINEMORE,10);
mMap.animateCamera(update);
}
// When click on this button, Map shows the place of Barista
public void onClick_Loc2(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_BARISTA,10);
mMap.animateCamera(update);
}
// To rmove All the Markers
public void onClick_Remove(View v){
mMap.clear();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
Aquí está mi archivo activity_maps.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:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
tools:context=".MapsActivity">
<Button
android:id="@+id/btnLoc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btnCity"
android:layout_alignParentTop="true"
android:onClick="onClick_Loc1"/>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@+id/btnLoc1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/btnCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btnLoc2"
android:layout_alignParentTop="true"
android:onClick="onClick_City"/>
<Button
android:id="@+id/btnLoc2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="onClick_Loc2"/>
<Button
android:id="@+id/removeMarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="onClick_Remove"/>
<CheckBox android:id="@+id/checkbox_restaurant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btnLoc1"
android:text="Restaurants"
android:onClick="onCheckboxClicked"/>
<CheckBox android:id="@+id/checkbox_hotels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkbox_restaurant"
android:text="Hotels"
android:onClick="onCheckboxClicked"/>
¿Cómo hago esto?
Gracias por adelantado.