(android地图开发) 高德地图自定义对话框

本文介绍了一种在Android应用中实现地图类型选择对话框的方法,包括卫星地图、平面地图和3D地图的选择,并展示了如何通过自定义对话框覆写Activity的onCreateDialog方法来实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

截图效果:

 

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal"
    android:background="#ffffff"
    >
    <!--卫星地图  -->
    <ImageButton
        android:id="@+id/satellite"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/main_map_mode_satellite_normal"        
        >   
    </ImageButton>
    <!--平面地图  -->

    <ImageButton
        android:id="@+id/plain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/main_map_mode_plain_normal" />

    <!--3d地图  -->
        <ImageButton
        android:id="@+id/d3"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/main_map_mode_3d_normal"        
        >   
    </ImageButton>

</LinearLayout>


自定义对话框覆写Activity的oncreatDialog()方法------核心代码

//自定义窗口(覆写 activity的oncreatedialog())
	@Override
	public Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder =new AlertDialog.Builder(this);  
		final AlertDialog customDialog;
		
		 LayoutInflater inflater = getLayoutInflater();   
		 View layout = inflater.inflate(R.layout.dialog, null);
		 builder.setTitle("地图类型选择");
		 builder.setView(layout);
		 customDialog=builder.create();
		 //相关点击事件处理
		 //卫星地图
		 ImageButton satellite=(ImageButton) layout.findViewById(R.id.satellite);
		 satellite.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tager_map_type=0;
				Message message=new Message();
				Bundle bundle=new Bundle();
				bundle.putInt("target_map_type", tager_map_type);
				message.setData(bundle);
				message.what=2;
				handler.sendMessage(message);
				//自定义对话框关闭
				customDialog.cancel();
			}
			 
		 });
		 //平面地图
		 ImageButton plain=(ImageButton) layout.findViewById(R.id.plain);
		 plain.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tager_map_type=1;
				Message message=new Message();
				Bundle bundle=new Bundle();
				bundle.putInt("target_map_type", tager_map_type);
				message.setData(bundle);
				message.what=2;
				handler.sendMessage(message);
				//自定义对话框关闭
				customDialog.cancel();
			}
			 
		 });
		 //3d地图
		 ImageButton d3=(ImageButton) layout.findViewById(R.id.d3);
		 d3.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tager_map_type=2;
				Message message=new Message();
				Bundle bundle=new Bundle();
				bundle.putInt("target_map_type", tager_map_type);
				message.setData(bundle);
				message.what=2;
				handler.sendMessage(message);
				//自定义对话框关闭
				customDialog.cancel();
			}
			 
		 });		
		
		return customDialog;
	}


MapView浮点按钮功能实现:

//是否显示路况信息标识
		private boolean target_traffic=true;
		//显示地图类型(0:卫星地图、1:平面地图、2:3d地图)
		private int tager_map_type=1;
	//功能代码的编写
	public void params(){
		//交通类型
		Button traffic=(Button) findViewById(R.id.traffic);
		traffic.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Message message=new Message();
				Bundle bundle=new Bundle();
				bundle.putBoolean("traffic",target_traffic);
				message.setData(bundle);
				message.what=1;
			    handler.sendMessage(message);				
			}
			
		});
		//地图类型(卫星地图、平面地图和三维地图)
		Button type=(Button) findViewById(R.id.type);
		type.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				//showDialog方法触发Activity的onCreateDialog(){可以自定义对话框样式}
				 showDialog(0);
				
			}
			
		});
		
	}


运行截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值