百度地图SDK for Android【Demo地图图层】

来源:互联网 发布:软件开发团队建设 编辑:程序博客网 时间:2024/04/27 13:02

http://blog.csdn.net/baidulbs/article/details/8569433

百度地图SDK为开发者提供了多种地图展示形式,用户可以通过相关的设置展示不同的地图图层,在这我将为大家介绍如何加载不同地图图层及各图层之间的切换。

        首先,我们要构建一个最基本的地图应用,具体介绍请参考:百度地图SDK for Android【Demo地图展示】

        在这个工程的基础之上我们做一定的修改。

        第一,修改布局文件,添加控制不同图层显示的button控件;具体代码如下:

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.       
  7.     <!-- 放入百度地图的mapview -->  
  8.     <com.baidu.mapapi.map.MapView android:id="@+id/bmapsView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:clickable="true"/>  
  12.   
  13.     <!-- 添加button,控制地图显示 -->  
  14.     <Button  
  15.         android:id="@+id/button1"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_alignParentRight="true"  
  19.         android:layout_alignParentTop="true"  
  20.         android:text="Button" />  
  21.   
  22. </RelativeLayout>  

        第二,在主类中初始化button对象、并设置相应的点击事件。不同图层的显示使用方法setSatellite和setTraffic来实现,具体代码如下所示:

[java] view plaincopy
  1. import com.baidu.mapapi.BMapManager;  
  2. import com.baidu.mapapi.map.MapController;  
  3. import com.baidu.mapapi.map.MapView;  
  4. import com.baidu.platform.comapi.basestruct.GeoPoint;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.app.Activity;  
  10.   
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     BMapManager bMapManager = null;  // 定义管理sdk的对象  
  15.     MapView mapView = null;  // 定义mapview对象  
  16.   
  17.     Button button;  
  18.     int flag = 0;  
  19.       
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.           
  24.         // 初始化管理对象,注意要在setContentView(R.layout.activity_main)之前初始化,否则会报错  
  25.         bMapManager = new BMapManager(getApplication());  
  26.         bMapManager.init("您申请的key"null);  
  27.           
  28.         setContentView(R.layout.activity_main);  
  29.           
  30.         // 初始化mapview对象,并且设置显示缩放控件  
  31.         mapView = (MapView) findViewById(R.id.bmapsView);  
  32.         mapView.setBuiltInZoomControls(true);  
  33.           
  34.         // 初始化button  
  35.         button = (Button) findViewById(R.id.button1);  
  36.         button.setText("点击进入《卫星图》");  
  37.           
  38.         // 定义地图控件,获取mapview的控制,并把地图范围定位北京市  
  39.         MapController mapController = mapView.getController();  
  40.         GeoPoint point =new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));  
  41.         mapController.setCenter(point);  
  42.         mapController.setZoom(12);  
  43.           
  44.         // 设置button的点击事件,根据点击次数的变化,分别设置显示普通地图、实时交通图和卫星图  
  45.         button.setOnClickListener(new OnClickListener()   
  46.         {  
  47.             @Override  
  48.             public void onClick(View v) {  
  49.                 // TODO Auto-generated method stub  
  50.                 if(flag == 0)  
  51.                 {  
  52.                     flag = flag + 1;  
  53.                     mapView.setSatellite(true);  
  54.                     button.setText("点击进入《实时交通图》");  
  55.                 }  
  56.                 else if(flag == 1)  
  57.                 {  
  58.                     flag = flag + 1;  
  59.                     mapView.setSatellite(false);  
  60.                     mapView.setTraffic(true);  
  61.                     button.setText("点击进入《普通地图》");  
  62.                 }  
  63.                 else if(flag == 2)  
  64.                 {  
  65.                     flag = 0;  
  66.                     mapView.setTraffic(false);  
  67.                     button.setText("点击进入《卫星图》");  
  68.                 }  
  69.             }  
  70.         });  
  71.           
  72.     }  
  73.       
  74.       
  75.     //注意在onResume、onDestroy和onPause中控制mapview和地图管理对象的状态  
  76.   
  77.     @Override  
  78.     protected void onResume() {  
  79.         // TODO Auto-generated method stub  
  80.         mapView.onResume();  
  81.         if(bMapManager!=null){  
  82.             bMapManager.start();  
  83.         }  
  84.         super.onResume();  
  85.     }  
  86.       
  87.     @Override  
  88.     protected void onDestroy() {  
  89.         // TODO Auto-generated method stub  
  90.         mapView.destroy();  
  91.         if(bMapManager!=null){  
  92.             bMapManager.destroy();  
  93.             bMapManager=null;  
  94.         }  
  95.         super.onDestroy();  
  96.     }  
  97.       
  98.     @Override  
  99.     protected void onPause() {  
  100.         // TODO Auto-generated method stub  
  101.         mapView.onPause();  
  102.         if(bMapManager!=null){  
  103.             bMapManager.stop();  
  104.         }  
  105.         super.onPause();  
  106.     }  
  107.   
  108. }  

        各图层的效果如下:

  

        点击下载原工程文件!

 

        小提示:百度地图SDK是支持地图旋转和3D特效的,双指扭转可实现地图旋转、双指下滑实现3D楼宇特效


更多详细信息请登录百度地图API官方网站:http://developer.baidu.com/map/
百度地图API论坛:http://bbs.lbsyun.baidu.com/



原创粉丝点击