Android 百度地图最新SDK v3.2.0和Android定位SDK:v5.0应用(2)
2015-01-30 17:09
513 查看
前面已经成功导入地图包,并成功运行,现在把代码改造一下,实现以下几个功能:
a 在百度地图中添加一个回到当前位置的按钮;
b 隐藏百度地图自带的放大缩小控件,实现自己的地图缩放控件;
c 换成最新的百度地图Android SDK v3.2.0和Android定位SDK:v5.0;
下面开始敲代码……
activity_location.xml
titlebar_bmap.xml
说明:以上就是添加了是地图视图回到当前定位位置的ImageView和自定义的地图放大缩小控件。
说明:
a 删除了百度地图demo中的部分代码;
b 隐藏百度地图自带放大缩小控件,以前的SDK是 mMapView.setBuiltInZoomControls(false)方法,而现在是:
c 百度地图视图回到当前定位位置的代码:
d 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) 设置扫描间隔,单位是毫秒
需要的话自行添加。
Demo下载地址:http://download.csdn.net/detail/yalinfendou/8410939
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);
d 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
Demo下载地址:http://download.csdn.net/detail/yalinfendou/8410939
相关文章推荐
- Android 百度地图最新SDK v3.2.0和Android定位SDK:v5.0应用(4)
- Android 百度地图最新SDK v3.2.0和Android定位SDK:v5.0应用(3)
- Android 百度地图最新SDK v3.2.0和Android定位SDK:v5.0应用(1)
- android使用百度地图、定位SDK实现地图和定位功能!(最新、可用+吐槽)
- android使用百度地图、定位SDK实现地图和定位功能!(最新、可用+吐槽)
- android使用百度地图、定位SDK实现地图和定位功能!(最新、可用+吐槽)
- android使用百度地图、定位SDK实现地图和定位功能!(最新、可用+吐槽)
- Android应用之——百度地图最新SDK3.0应用,实现最常用的标注覆盖物以及弹出窗覆盖物
- 我的Android进阶之旅------>Android百度地图定位SDK功能学习
- Android定位SDK_百度地图开发
- 百度地图定位SDKv4.2版本(android)的使用教程
- Android百度地图(一):百度地图定位sdk 类方法参数、定位原理详细介绍
- Android百度地图导航SDK2.0 定位SDK6.0.5的导航参数设置方法
- Android百度地图使用之定位和地理、反地理编码<基于定位SDK4.2>
- 高德地图Android版SDK的应用(定位,添加自定义标记点)
- 百度地图Android定位SDK的API
- Android百度地图(一):百度地图定位sdk 类方法参数、定位原理详细介绍
- Android使用百度地图的SDK获取定位
- 百度地图植入android应用系列教程二实现百度地图定位及方向传感器
- Android应用之——百度地图定位返回4.9E-324的解决方案