I want to do something like what was done right here in Android. What I need to do is group markers in different groups and check boxes for each group.
When I check the boxes and check the boxes, the markers should show and hide.
Here is my MapsActivity.java file.
public class MapsActivity extends FragmentActivity {
private final LatLng LOCATION_DINEMORE = new LatLng(6.9270786,79.861243);
private final LatLng LOCATION_BARISTA = new LatLng(6.0334009,80.218384);
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
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");
mMap.addMarker(new MarkerOptions()
.position(LOCATION_DINEMORE)
.title("Dinemore")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
);
mMap.addMarker(new MarkerOptions()
.position(LOCATION_BARISTA)
.title("Barista Lavazza")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
);
mMap.addMarker(new MarkerOptions()
.position(LOCATION_AVENRA)
.title("Avenra Garden")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
);
mMap.addMarker(new MarkerOptions()
.position(LOCATION_MOUNTBATTEN)
.title("Mounbatten Bunglow")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
);
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);
}
public void onClick_Loc1(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_DINEMORE,10);
mMap.animateCamera(update);
}
public void onClick_Loc2(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_BARISTA,10);
mMap.animateCamera(update);
}
public void onClick_Remove(View v){
mMap.clear();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
Here is my activity_maps.xml file.
<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"/>
How to do it?
Thanks in advance.
user5259714
source
share