Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an android map menu to change map type

I have a map in my android app. By default it shows the satellite view, but I have turned it off to only show the road maps view. However, I am wondering how I would construct a menu so when the user pressed the menu button, it would display a section at the bottom with 'toggle satellite map'. (I will be adding other items to the menu in the future)

THanks to anyone who can help with this

like image 393
Steve Avatar asked Aug 15 '11 12:08

Steve


People also ask

Can you customize Google Maps API?

Create a map with the Google Maps API.Google Maps Platform gives you the ability to create a truly custom map that works exactly how you want it to.

Can you style Google Maps API?

With style options you can customize the presentation of the standard Google map styles, changing the visual display of features like roads, parks, businesses, and other points of interest. As well as changing the style of these features, you can hide features entirely.


1 Answers

This is my implementation that works just fine with GoogleMaps API v2. It shows a dialog with four radio buttons from which you can select a map type. The currently selected map type is also already selected.

Android AlertDialog to select GoogleMaps MapType

This code preferably goes into your activity that holds the map. Just make sure your map is initiated and displays correctly before you call showMapTypeSelectorDialog(). I also recommend using Resource Strings for the labels.

private GoogleMap mMap;

...

private static final CharSequence[] MAP_TYPE_ITEMS =
        {"Road Map", "Hybrid", "Satellite", "Terrain"};

private void showMapTypeSelectorDialog() {
    // Prepare the dialog by setting up a Builder.
    final String fDialogTitle = "Select Map Type";
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(fDialogTitle);

    // Find the current map type to pre-check the item representing the current state.
    int checkItem = mMap.getMapType() - 1;

    // Add an OnClickListener to the dialog, so that the selection will be handled.
    builder.setSingleChoiceItems(
            MAP_TYPE_ITEMS,
            checkItem,
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int item) {
                    // Locally create a finalised object.

                    // Perform an action depending on which item was selected.
                    switch (item) {
                        case 1:
                            mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                            break;
                        case 2:
                            mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                            break;
                        case 3:
                            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                            break;
                        default:
                            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    }
                    dialog.dismiss();
                }
            }
    );

    // Build the dialog and show it.
    AlertDialog fMapTypeDialog = builder.create();
    fMapTypeDialog.setCanceledOnTouchOutside(true);
    fMapTypeDialog.show();
}
like image 173
easytarget Avatar answered Nov 20 '22 07:11

easytarget