`
qinweiping
  • 浏览: 128183 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

baiduMap开发(2)BaiduMapView详解及布局实现

 
阅读更多

今天来了解一下BaiduMap里的一些关于地图显示的知识 和Android中如何设置横屏。如何打开GPS定位相关知识。顺便把我的毕业设计老师要求的布局格式实现一下,界面做的不是很好看,大家帮我改进。代码写的也有点凌乱,还望高手提提意见!如何将类封装使得可扩展性得到充分发挥!

    首先要了解一下BaiduMap中的MapView 百度地图是通过MapView来进行显示的,我个人的理解:MapView应该和Android中的View是同一个概念。在你渲染地图之前,要首先创建MapView对象,切记:新版的地图不是直接创建MapVIew,在创建MapView之前还要加载地图管理器。baiduMap里还有一个地图控制器:MapController。本人查看了BaiduMap官方api, MapController中主要提供了一些关于地图移动,地图加载和手势控制的一些关于控制地图行为的函数。此外,一张地图的控制器是可以通过mapView.getController()来获取该地图的控制器的。

    关于Android内置的Location定位相关的函数:你可以通过触发Android内置的关于Location函数的事件可以来实时获取当前的位置 。

    我这里是通过一个函数 protected whereAmI()来获取当前的经纬度坐标值来实现定位的。用的不是BaiduMap里的位置定位,而是Android自带的LocationManager的位置定位。

    主布局页面是一张baidu地图,你可以通过点击XML页面上的按钮来实现相关的功能。

    先来说说我写的关于当前位置的函数WhereAmI()

 

  protected void whereAmI()  
            {  
                lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
                  
                  LocationListener mLocationListener = new LocationListener()  
                {  
  
                    @Override  
                    public void onLocationChanged(Location location) {  
  
                        //1 获取经度   
                        double lat = location.getLongitude();  
                        //2 获取纬度值  
                        double lng = location.getLongitude();  
                        //3 获取GeoPoint   
                        GeoPoint gp = new GeoPoint((int)(lat*1E6),(int)(lng*1E6));  
                        //4 添加到地图控制器中   
                        mc.animateTo(gp);  
                    }  
  
                    @Override  
                    public void onProviderDisabled(String provider) {  
                        // TODO Auto-generated method stub  
                          
                    }  
  
                    @Override  
                    public void onProviderEnabled(String provider) {  
                        // TODO Auto-generated method stub  
                          
                    }  
  
                    @Override  
                    public void onStatusChanged(String provider, int status,  
                            Bundle extras) {  
                        // TODO Auto-generated method stub  
                          
                    }  
                      
                };  
                  
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,mLocationListener);  
                  
            }  
        });  

 

 当然此函数写的还不完善::若是碰到这个情况,你一开始并没有打开GPS相关的设备,那在函数中应该做相应的判断,这个在后续的版本中改进:

当前页面的布局:

 

 

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="@drawable/dao_bg"  
    android:orientation="horizontal" >  
  
    <!-- 此处放一个MapView 用来显示地图 -->  
  
    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_weight="2"  
        android:gravity="center"  
        android:orientation="vertical" >  
          
    <com.baidu.mapapi.map.MapView  
            android:id="@+id/bmapView"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:clickable="true" />   
    </LinearLayout>  
  
    <!-- 用一个Layout 存放右边的一排按钮集合  -->  
    <LinearLayout  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_weight="1"  
        android:gravity="center"  
        android:orientation="vertical" >  
  
        <!-- 当前景点 -->  
  
        <LinearLayout  
            android:id="@+id/curbutton"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="5dp"  
            android:background="@drawable/bg_delwords"  
            android:orientation="horizontal" >  
  
            <ImageView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="left|center_vertical"  
                android:layout_marginLeft="5dp"  
                android:src="@drawable/current" />  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center"  
                android:layout_marginLeft="2dp"  
                android:text="@string/curbuttonText"  
                android:textColor="@color/black" />  
        </LinearLayout>  
  
        <!-- 所有景点 -->  
  
        <LinearLayout  
            android:id="@+id/allbutton"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="7dp"  
            android:background="@drawable/bg_delwords"  
            android:orientation="horizontal" >  
  
            <ImageView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="left|center_vertical"  
                android:layout_marginLeft="5dp"  
                android:src="@drawable/all" />  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center"  
                android:layout_marginLeft="2dp"  
                android:text="@string/allbuttonText"  
                android:textColor="@color/black" />  
        </LinearLayout>  
        <!-- 锁定位置 -->  
  
        <LinearLayout  
            android:id="@+id/suoding"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="7dp"  
            android:background="@drawable/bg_delwords"  
            android:orientation="horizontal" >  
  
            <ImageView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="left|center_vertical"  
                android:layout_marginLeft="5dp"  
                android:src="@drawable/location" />  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center"  
                android:layout_marginLeft="2dp"  
                android:text="@string/suoding"  
                android:textColor="@color/black" />  
        </LinearLayout>  
        <!-- 照相机 -->  
  
        <LinearLayout  
            android:id="@+id/camera"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="7dp"  
            android:background="@drawable/bg_delwords"  
            android:orientation="horizontal" >  
  
            <ImageView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="left|center_vertical"  
                android:layout_marginLeft="5dp"  
                android:src="@drawable/takecamera" />  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center"  
                android:layout_marginLeft="2dp"  
                android:text="@string/camera"  
                android:textColor="@color/black" />  
        </LinearLayout>  
        <!-- 更多 -->  
  
        <LinearLayout  
            android:id="@+id/more"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="7dp"  
            android:background="@drawable/bg_delwords"  
            android:orientation="horizontal" >  
  
            <ImageView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="left|center_vertical"  
                android:layout_marginLeft="5dp"  
                android:src="@drawable/more" />  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center"  
                android:layout_marginLeft="2dp"  
                android:text="@string/more"  
                android:textColor="@color/black" />  
        </LinearLayout>  
  
        <!-- 退出 -->  
  
        <LinearLayout  
            android:id="@+id/exit"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginTop="7dp"  
            android:background="@drawable/bg_delwords"  
            android:orientation="horizontal" >  
  
            <ImageView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="left|center_vertical"  
                android:layout_marginLeft="5dp"  
                android:src="@drawable/exit" />  
  
            <TextView  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="center"  
                android:layout_marginLeft="2dp"  
                android:text="@string/exit"  
                android:textColor="@color/black" />  
        </LinearLayout>  
    </LinearLayout>  
  
</LinearLayout>  

 

界面布局图如下所示:



 

 

主要Java层代码:

 

 

 

public class MainActivity extends Activity {  
  
    public static final String TAG = "MainActivity";  
  
    /* 
     * baiduMap相关的一些类 
     */  
    // MapView  
    public MapView mMapView = null;  
      
    //声明地图控制器   
    public MapController mc = null;  
  
  
    // 地图控制器  
    private MapController mMapController = null;  
  
    // 地图显示事件监听器  
    private MKMapViewListener mMapListener = null;  
  
  
    // 屏幕的高度  
    private static float screenHeight;  
    // 屏幕的宽度  
    private static float screenWidth;  
  
    // Android定位相关  
    private LocationManager lm;// 位置管理监听器  
    private LocationListener mLocationListener;// 位置变化监听器  
  
  
    // 定位相关  
    LocationClient mLocClient;  
    // 地图覆盖物  
    MyLocationOverlay myLocationOverlay = null;  
  
    // 当前经纬度  
    private GeoPoint myPoint;  
  
  
    // 存储资源路径  
    SharedPreferences sp_filePath;  
  
    /*private PopupWindow m_popupWindow;*/  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        // 设置横屏  
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
        DisplayMetrics dm = new DisplayMetrics();  
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);  
        screenHeight = dm.heightPixels;// 获得屏幕高度  
        screenWidth = dm.widthPixels;// 获得屏幕宽度  
        // 获取sharedPreferences中获取标志值,判断是否是本次运行中第一次打开主界面  
  
        SharedPreferences sp_isOpen = this.getSharedPreferences("isOpen",  
                Context.MODE_PRIVATE);// 只有本程序可访问模式打开  
        boolean isOpen = sp_isOpen.getBoolean("isOpen", true);// 若无存储值,默认是true,是第一次打开  
        sp_filePath = getSharedPreferences("filePath", Context.MODE_PRIVATE);  
    /*  filePath = sp_filePath.getString("filePath", null);*/  
  
        String serviceName = Context.LOCATION_SERVICE;// 需要的服务名称  
        lm = (LocationManager) this.getSystemService(serviceName);// 获取位置管理器实例  
        // 提示打开GPS  
        if (isOpen  
                && lm.isProviderEnabled(LocationManager.GPS_PROVIDER) != true)// 判断当前GPS是否打开  
        {  
            Log.d(TAG, "GPS is open>>>" + isOpen);  
            Intent callGPSSettingIntent = new Intent(  
                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
            startActivity(callGPSSettingIntent);  
            Toast.makeText(this, getResources().getString(R.string.opengps),  
                    Toast.LENGTH_LONG).show();  
            SharedPreferences.Editor editor = sp_isOpen.edit();  
            editor.putBoolean("isOpen", false);  
            editor.commit();  
        }  
  
        DemoApplication app = (DemoApplication) this.getApplication();  
  
        if (app.mBMapManager == null) {  
            app.mBMapManager = new BMapManager(getApplication());  
  
            app.mBMapManager.init(new DemoApplication.MyGeneralListener());  
        }  
  
      
        setContentView(R.layout.main);  
          
        if (mMapView == null) {  
            mMapView = (MapView) findViewById(R.id.bmapView);  
  
        }  
          
        mMapController = mMapView.getController();  
  
        // 设置启动内置缩放控件  
        mMapView.setBuiltInZoomControls(true);  
        // 设置缩放时重绘覆盖物  
        // /.......?  
        mMapController.enableClick(true);  
        // 设置地图中心点  
        // mMapController.animateTo(arg0);  
        // 设置地图缩放比例  
        mMapController.setZoom(15);  
        // mMapController.  
        // 添加定位图层  
        myLocationOverlay = new MyLocationOverlay(mMapView);  
  
        // 添加定位覆盖物  
        mMapView.getOverlays().add(myLocationOverlay);  
  
        // 添加定位浮标  
        Drawable maker = getResources().getDrawable(R.drawable.location);  
  
        myLocationOverlay.setMarker(maker);  
  
        mLocationListener = new LocationListener() {  
  
            @Override  
            public void onStatusChanged(String provider, int status,  
                    Bundle extras) {  
  
            }  
  
            @Override  
            public void onProviderEnabled(String provider) {  
                // TODO Auto-generated method stub  
  
            }  
  
            @Override  
            public void onProviderDisabled(String provider) {  
                // TODO Auto-generated method stub  
  
            }  
  
            @Override  
            public void onLocationChanged(Location location) {  
  
                if (null != location) {  
  
                    double Longitude = location.getLongitude();// 获取经度  
                    double Latitude = location.getLatitude();// 获取纬度  
                    myPoint = new GeoPoint((int) (Longitude * 1E6),  
                            (int) (Latitude * 1E6));  
  
                    mMapController.animateTo(myPoint);  
                    // 设置地图中心点  
                    mMapController.setCenter(myPoint);  
                    // 设定比例尺  
                    mMapController.setZoom(16);  
                }  
            }  
        };  
  
        // 获取当前景点按钮的引用  
        LinearLayout BtnCurrent = (LinearLayout) findViewById(R.id.curbutton);  
        BtnCurrent.setOnClickListener(new OnClickListener() {  
  
            public void onClick(View v) {  
            /*  // Log.d(TAG,"btnCurrent is Click"); 
                mMapView.getOverlays().clear(); 
                mMapView.getOverlays().add(myLocationOverlay); 
*/  
                  
                whereAmI();  
            }  
  
            //当前处于某位置的函数   
            protected void whereAmI()  
            {  
                lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
                  
                  LocationListener mLocationListener = new LocationListener()  
                {  
  
                    @Override  
                    public void onLocationChanged(Location location) {  
  
                        //1 获取经度   
                        double lat = location.getLongitude();  
                        //2 获取纬度值  
                        double lng = location.getLongitude();  
                        //3 获取GeoPoint   
                        GeoPoint gp = new GeoPoint((int)(lat*1E6),(int)(lng*1E6));  
                        //4 添加到地图控制器中   
                        mc.animateTo(gp);  
                    }  
  
                    @Override  
                    public void onProviderDisabled(String provider) {  
                        // TODO Auto-generated method stub  
                          
                    }  
  
                    @Override  
                    public void onProviderEnabled(String provider) {  
                        // TODO Auto-generated method stub  
                          
                    }  
  
                    @Override  
                    public void onStatusChanged(String provider, int status,  
                            Bundle extras) {  
                        // TODO Auto-generated method stub  
                          
                    }  
                      
                };  
                  
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,mLocationListener);  
                  
            }  
        });  
  
        // 获取所有景点按钮的引用  
        LinearLayout BtnAll = (LinearLayout) findViewById(R.id.allbutton);  
        BtnAll.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                // Intent intent = new Intent();  
                Log.d(TAG, "btnAll is Click");  
            }  
        });  
  
        // 获取锁定位置按钮的引用  
        LinearLayout BtnLock = (LinearLayout) findViewById(R.id.suoding);  
        BtnLock.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                Log.d(TAG, "btnLock is Click ");  
            }  
  
        });  
  
        // 获取拍照按钮的引用  
        LinearLayout BtnCamera = (LinearLayout) findViewById(R.id.camera);  
        BtnCamera.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                Log.d(TAG, "btn BtnCamera is Click ");  
            }  
  
        });  
  
        // 获取更多按钮的引用  
        LinearLayout BtnMore = (LinearLayout) findViewById(R.id.more);  
        BtnMore.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                Log.d(TAG, "btn BtnMore is Click ");  
            }  
        });  
  
        // 获取退出按钮的引用  
        LinearLayout BtnExit = (LinearLayout) findViewById(R.id.exit);  
        BtnExit.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                Log.d(TAG, "btn BtnExit is Click ");  
            }  
        });  
    }  
  
  
    public class MyLocationListener implements BDLocationListener {  
  
        @Override  
        public void onReceiveLocation(BDLocation location) {  
  
            if (location == null)  
                return;  
            // 获取纬度值  
            locData.latitude = location.getLatitude();  
            // 获取经度值  
            locData.longitude = location.getLongitude();  
  
            // 如果不显示定位精度圈,将accuracy赋值为0即可  
            locData.accuracy = location.getRadius();  
            // 此处可以设置 locData的方向信息, 如果定位 SDK 未返回方向信息,用户可以自己实现罗盘功能添加方向信息。  
            locData.direction = location.getDerect();  
  
            // 更新定位数据  
            myLocationOverlay.setData(locData);  
            // 更新图层数据执行刷新后生效  
            mMapView.refresh();  
  
        }  
  
        public class locationOverlay extends MyLocationOverlay {  
  
            public locationOverlay(MapView mapView) {  
                super(mapView);  
            }  
  
            @Override  
            protected boolean dispatchTap() {  
                // TODO Auto-generated method stub  
                // 处理点击事件,弹出泡泡  
                popupText.setBackgroundResource(R.drawable.popup);  
                popupText.setText("我的位置");  
                pop.showPopup(BMapUtil.getBitmapFromView(popupText),  
                        new GeoPoint((int) (locData.latitude * 1e6),  
                                (int) (locData.longitude * 1e6)), 8);  
                return true;  
  
            }  
  
        }  
          
      
          
  
    /*  // 标注一个遮障物  
        public class MyOverlay extends Overlay {  
            // 声明一个地点  
            private GeoPoint geoPoint = new GeoPoint((int) (39.915 * 1E6),  
                    (int) (116.404 * 1E6));  
            // 声明一个画笔工具  
            private Paint paint = new Paint();  
  
            public void draw(Canvas arg0, MapView arg1, boolean arg2) {  
  
            }  
        }  
  
        @Override  
        public void onReceivePoi(BDLocation poiLocation) {  
  
            if (poiLocation == null) {  
                return;  
            }  
        }  
  
    }  
  
    @Override  
    protected void onPause() {  
        mMapView.onPause();  
        super.onPause();  
    }  
  
    @Override  
    protected void onResume() {  
        mMapView.onResume();  
        super.onResume();  
    }  
  
    @Override  
    protected void onDestroy() {  
        // 退出时销毁定位  
        if (mLocClient != null)  
            mLocClient.stop();  
        mMapView.destroy();  
        super.onDestroy();  
    }  
  
    @Override  
    protected void onSaveInstanceState(Bundle outState) {  
        super.onSaveInstanceState(outState);  
        mMapView.onSaveInstanceState(outState);  
  
    }  
  
    @Override  
    protected void onRestoreInstanceState(Bundle savedInstanceState) {  
        super.onRestoreInstanceState(savedInstanceState);  
        mMapView.onRestoreInstanceState(savedInstanceState);  
    }  
}  

 

<!--EndFragment-->

 

<!--EndFragment-->

 

 

 

 

<!--EndFragment-->
  • 大小: 491.2 KB
0
0
分享到:
评论
1 楼 枫磊行者 2016-01-27  

相关推荐

Global site tag (gtag.js) - Google Analytics