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






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值