制作Android地图菜单以更改地图类型 [英] Making an android map menu to change map type

查看:163
本文介绍了制作Android地图菜单以更改地图类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的android应用中有一张地图。默认情况下,它显示卫星视图,但我已将其关闭以仅显示路线图视图。但是,我想知道如何构建一个菜单,所以当用户按下菜单按钮时,它会在底部显示切换卫星地图部分。 (我将来会在菜单中添加其他项目)

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

推荐答案

这是我的实现,它适用于GoogleMaps API v2。它显示一个包含四个单选按钮的对话框,您可以从中选择地图类型。当前选择的地图类型也已被选中。

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.

此代码最好进入保存地图的活动。在调用showMapTypeSelectorDialog()之前,请确保您的地图已启动并正确显示。我还建议使用资源字符串作为标签。

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();
}

这篇关于制作Android地图菜单以更改地图类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆