gpt4 book ai didi

android - 制作一个android map 菜单来改变 map 类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:05 24 4
gpt4 key购买 nike

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

感谢任何可以提供帮助的人

最佳答案

这是我的实现,可以很好地与 GoogleMaps API v2 配合使用。它显示了一个带有四个单选按钮的对话框,您可以从中选择 map 类型。当前选择的 map 类型也已被选择。

Android AlertDialog to select GoogleMaps MapType

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

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 - 制作一个android map 菜单来改变 map 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7064857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com