I'm currently making a Campus Map using Google Maps API v2. I have currently 10 map markers. Each of them has its info Window. What I want to happen is that when the user clicks on the info Window, it shows a Listview Custom Dialog. I wanna use the if else statement but I don't know how to construct since I haven't found any examples on the Internet.
This is my Dialog Activity
public class AdminDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.layers)
.setItems(R.array.layer_options, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
}
And I have some sample markers...
public void addMarkersToMap() {
Marker cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
Marker adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Marker casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
My concern is how to construct an if else statement or a switch case statement using this code.. this is where the Custom Dialog is called..
adminDialog = new AdminDialog();
adminDialog.show(getFragmentManager(), "custom-tag-goes-here");
declare your markers as fields, instead of local varaibles
Marker cmumarker , adminmarker, casmarker;
// You can also keep the ids
String cmumarkerId , adminmarkerId, casmarkerID;
public void addMarkersToMap() {
cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
cmumarkerId=cmumarker.getID();
adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
adminmarkerId=adminmarker.getID();
casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
casmarkerId=casmarker.getID();
}
set the click listener to the markers, for example after you create the map, or at the end of addMarkersToMap()
map.setOnMarkerClickListener(this);
override the onMarkerClick(Marker marker)
function, and make your decisions
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.getId().equals(cmumarkerId)) {
//do whatever you want
return true;
}
if (marker.getId().equals(adminmarkerId)) {
//do whatever you want
return true;
}
if (marker.getId().equals(casmarkerId)) {
//do whatever you want
return true;
}
return false;
}
Also, if you want the info window to be clicable, instead of the marker, you
public void onInfoWindowClick(Marker marker)
instead of the onMarkerClickmap.setOnInfoWindowClickListener(this);
instead of map.setOnMarkerClickListener(this);UPDATE:
If you have many markers, you can look for a better way of keeping the ids. Some people uses to compare directly the marker, by reference(ie. if (marker==adminmarker{...}) but sometimes it happens to be diferent objects with the same values, so the id is the most secure way of comparing them.