之前只是簡單集成百度地圖,但是並沒有實現我們平時想要的功能。
譬如點擊獲取定位,切換路況交通圖,衛星圖,對地圖的縮放等
佈局
<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)));