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

本文介绍了如何使用百度地图Android SDK v3.2.0及定位SDK v5.0开发地图应用,包括添加返回当前位置按钮、自定义地图缩放控件等功能。

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

前面已经成功导入地图包,并成功运行,现在把代码改造一下,实现以下几个功能:

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






基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员用户两个主要模块。管理员负责视频、文件文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值