Android 百度地圖最新SDK v3.2.0和Android定位SDK:v5.0應用(2)

前面已經成功導入地圖包,併成功運行,現在把代碼改造一下,實現以下幾個功能:

a 在百度地圖中添加一個回到當前位置的按鈕;

b 隱藏百度地圖自帶的放大縮小控件,實現自己的地圖縮放控件;

c 換成最新的百度地圖Android SDK v3.2.0和Android定位SDK:v5.0;

下面開始敲代碼……

1.首先創建地圖佈局

activity_location.xml
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5F7FA" >

    <include
        android:id="@+id/lin_top"
        layout="@layout/titlebar_bmap" />

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/lin_top"
        android:clickable="true" />

    <LinearLayout
        android:id="@+id/plus_layout"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="60dp"
        android:background="@drawable/zoom_selector"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="1dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/sub_layout"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/zoom_selector"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="1dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/docenter_layout"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="50dp"
        android:background="@drawable/searchbg"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="3dp" >

        <ImageView
            android:id="@+id/docenter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="1dp"
            android:background="@drawable/center_drvier_selector" />
    </LinearLayout>

</RelativeLayout>


titlebar_bmap.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="#808080" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:text="百度地圖使用"
        android:textColor="#000000"
        android:textSize="20sp" />

</RelativeLayout>


說明:以上就是添加了是地圖視圖回到當前定位位置的ImageView和自定義的地圖放大縮小控件。


2.代碼實現


/**
 * 百度地圖使用
 * 
 * @author wen
 * @version 2015年1月27日
 * @see BaseMapActivity
 * @since
 */
public class BaseMapActivity extends Activity implements OnClickListener {

	private static final String TAG = BaseMapActivity.class.getSimpleName();;
	// 定位相關
	LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	MapView mMapView;
	BaiduMap mBaiduMap;

	// 自定義UI
	private ImageView imv_doCenter;
	private LinearLayout plus_layout;// +
	private LinearLayout sub_layout;// -

	private float zoomLevel = 14f;// 地圖縮放級別
	boolean isFirstLoc = true;// 是否首次定位
	/**
	 * 當前位置經緯度
	 */
	private double mCurrentLantitude = 0.0;
	private double mCurrentLongitude = 0.0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// NO_TITLE
		setContentView(R.layout.activity_location);
		initBaidu();
		setupView();
		setLinstener();

	}

	public void setupView() {

		imv_doCenter = (ImageView) findViewById(R.id.docenter);
		plus_layout = (LinearLayout) findViewById(R.id.plus_layout);
		sub_layout = (LinearLayout) findViewById(R.id.sub_layout);
	}

	public void setLinstener() {
		imv_doCenter.setOnClickListener(this);
		plus_layout.setOnClickListener(this);
		sub_layout.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.docenter:
			if (mCurrentLantitude != 0 && mCurrentLongitude != 0) {
				LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude);
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaiduMap.animateMapStatus(u);
			}

			break;

		case R.id.plus_layout:
			zoomLevel += 0.9f;
			// 設置地圖的縮放比例
			MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(zoomLevel);
			mBaiduMap.setMapStatus(msu);
			break;

		case R.id.sub_layout:
			zoomLevel -= 0.9f;
			MapStatusUpdate msu1 = MapStatusUpdateFactory.zoomTo(zoomLevel);
			mBaiduMap.setMapStatus(msu1);
			// mBaiduMap.animateMapStatus(msu1);
			break;
		default:
			break;

		}

	}

	public void initBaidu() {

		// 地圖初始化
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		// 開啓定位圖層
		mBaiduMap.setMyLocationEnabled(true);
		// 定位初始化
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打開gps
		option.setCoorType("bd09ll"); // 設置座標類型
		option.setScanSpan(1000);
		mLocClient.setLocOption(option);
		mLocClient.start();

		hideZoomControls();
	}

	/**
	 * 隱藏百度地圖自帶放大縮小控件,原來是mMapView.setBuiltInZoomControls(false);
	 */

	public void hideZoomControls() {
		int count = mMapView.getChildCount();
		for (int i = 0; i < count; i++) {
			View child = mMapView.getChildAt(i);
			if (child instanceof ZoomControls) {
				child.setVisibility(View.INVISIBLE);
			}
		}
	}

	/**
	 * 定位SDK監聽函數
	 */
	public class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			// map view 銷燬後不在處理新接收的位置
			if (location == null || mMapView == null)
				return;
			MyLocationData locData = new MyLocationData.Builder()
					.accuracy(location.getRadius())
					// 此處設置開發者獲取到的方向信息,順時針0-360
					.direction(100).latitude(location.getLatitude())
					.longitude(location.getLongitude()).build();
			mBaiduMap.setMyLocationData(locData);

			mCurrentLantitude = location.getLatitude();
			mCurrentLongitude = location.getLongitude();
			if (isFirstLoc) {
				isFirstLoc = false;
				LatLng ll = new LatLng(location.getLatitude(),
						location.getLongitude());
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaiduMap.animateMapStatus(u);
			}
		}

		public void onReceivePoi(BDLocation poiLocation) {
		}
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// 退出時銷燬定位
		mLocClient.stop();
		// 關閉定位圖層
		mBaiduMap.setMyLocationEnabled(false);
		mMapView.onDestroy();
		mMapView = null;
		super.onDestroy();
	}

}

說明:
a 刪除了百度地圖demo中的部分代碼;
b 隱藏百度地圖自帶放大縮小控件,以前的SDK是 mMapView.setBuiltInZoomControls(false)方法,而現在是:
/**
	 * 隱藏百度地圖自帶放大縮小控件,原來是mMapView.setBuiltInZoomControls(false);
	 */

	public void hideZoomControls() {
		int count = mMapView.getChildCount();
		for (int i = 0; i < count; i++) {
			View child = mMapView.getChildAt(i);
			if (child instanceof ZoomControls) {
				child.setVisibility(View.INVISIBLE);
			}
		}
	}

c 百度地圖視圖回到當前定位位置的代碼:
LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude);
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaiduMap.animateMapStatus(u);

LocationClientOption中的方法介紹:
getAddrType() 獲取地址信息設置
getCoorType()  獲得當前設置的座標類型
getLocationMode()   獲取當前的定位模式
getScanSpan()  獲取 設置的掃描間隔,單位是毫秒       
isOpenGps()  是否打開gps進行定位       
setCoorType(java.lang.String coorType)  設置座標類型      
setIgnoreKillProcess(boolean killProcess)  設置是否退出定位進程        
setIsNeedAddress(boolean isNeed)  設置是否需要地址信息,默認爲無地址      
setLocationMode(LocationClientOption.LocationMode mode)   設置定位模式 
setNeedDeviceDirect(boolean isNeedDeviceDirect)   在網絡定位時,是否需要設備方向       
setOpenGps(boolean openGps)   是否打開gps進行定位       
setScanSpan(int scanSpan)    設置掃描間隔,單位是毫秒
需要的話自行添加。

3.換成最新的百度地圖Android SDK v3.2.0和Android定位SDK:v5.0






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章