之前只是简单集成百度地图,但是并没有实现我们平时想要的功能。
譬如点击获取定位,切换路况交通图,卫星图,对地图的缩放等
布局
<RelativeLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baidu.mapapi.map.MapView
android:id="@+id/bd_map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<LinearLayout
android:id="@+id/ll_scale_control"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/uniform_margin"
android:background="@drawable/bg_btn_map"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_plus"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="@dimen/padding_10"
android:src="@drawable/ic_map_plus" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/gray_13" />
<ImageView
android:id="@+id/iv_minus"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="@dimen/padding_10"
android:src="@drawable/ic_map_minus" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_traffic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/ll_scale_control"
android:layout_marginBottom="@dimen/margin_10"
android:layout_marginLeft="@dimen/uniform_margin"
android:background="@drawable/bg_btn_map"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_traffic"
android:layout_width="40dp"
android:layout_height="40dp"
android:paddingLeft="@dimen/padding_10"
android:paddingRight="@dimen/padding_10"
android:paddingTop="@dimen/padding_10"
android:src="@drawable/ic_map_traffic_close" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingBottom="@dimen/padding_10"
android:text="路况"
android:textSize="@dimen/textSize10" />
</LinearLayout>
<ImageView
android:id="@+id/iv_view"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="@dimen/margin_10"
android:layout_above="@id/ll_traffic"
android:layout_marginLeft="@dimen/uniform_margin"
android:layout_marginTop="@dimen/margin_20"
android:background="@drawable/bg_btn_map"
android:padding="@dimen/padding_10"
android:src="@drawable/ic_map_view" />
<ImageView
android:id="@+id/iv_location"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@id/ll_scale_control"
android:layout_marginLeft="@dimen/uniform_margin"
android:layout_marginTop="@dimen/margin_20"
android:background="@drawable/bg_btn_map"
android:padding="@dimen/padding_10"
android:src="@drawable/ic_map_my_location" />
</RelativeLayout>
获取定位
isFirstLoc = true;
// 开启定位图层
//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();
定位监听函数
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || bdMapView == null) {
return;
}
mCurrentLat = location.getLatitude();
mCurrentLon = location.getLongitude();
mCurrentAccracy = location.getRadius();
locData = new MyLocationData.Builder()
.accuracy(300)
//.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(mCurrentDirection).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
}
切换路况交通图
if (!isTrafficOpen) {
showToast("实时路况打开");
mBaiduMap.setTrafficEnabled(true);
ivTraffic.setImageResource(R.drawable.ic_map_traffic_open);
isTrafficOpen = !isTrafficOpen;
} else {
showToast("实时路况关闭");
mBaiduMap.setTrafficEnabled(false);
ivTraffic.setImageResource(R.drawable.ic_map_traffic_close);
isTrafficOpen = !isTrafficOpen;
}
切换卫星图
if (!isViewcSwitch) {
showToast("切换卫星视图");
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
isViewcSwitch = !isViewcSwitch;
} else {
showToast("卫星视图关闭");
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
isViewcSwitch = !isViewcSwitch;
}
缩放
if (mCurrentZoomLevel < 7) {
ivMinus.setEnabled(false);
ivMinus.setImageResource(R.drawable.ic_map_minus_gray);
} else {
mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut());
ivPlus.setEnabled(true);
ivPlus.setImageResource(R.drawable.ic_map_plus);
ivMinus.setEnabled(true);
ivMinus.setImageResource(R.drawable.ic_map_minus);
}
修改自定义marker
// 修改为自定义marker
mCurrentMarker = BitmapDescriptorFactory
.fromResource(R.drawable.ic_map_location);
mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker,
getMyColor(R.color.mapColor), getMyColor(R.color.mapColor)));