之前只是简单集成百度地图,但是并没有实现我们平时想要的功能。
譬如点击获取定位,切换路况交通图,卫星图,对地图的缩放等
布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| <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>
|
获取定位
1 2 3 4 5 6 7 8 9 10 11 12 13
| isFirstLoc = true;
mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); option.setCoorType("bd09ll"); option.setScanSpan(1000); mLocClient.setLocOption(option); mLocClient.start();
|
定位监听函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
public class MyLocationListenner implements BDLocationListener {
@Override public void onReceiveLocation(BDLocation location) {
if (location == null || bdMapView == null) { return; } mCurrentLat = location.getLatitude(); mCurrentLon = location.getLongitude(); mCurrentAccracy = location.getRadius(); locData = new MyLocationData.Builder() .accuracy(300)
.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())); } }
|
切换路况交通图
1 2 3 4 5 6 7 8 9 10 11 12
| 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; }
|
切换卫星图
1 2 3 4 5 6 7 8 9 10 11
| if (!isViewcSwitch) { showToast("切换卫星视图"); mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); isViewcSwitch = !isViewcSwitch; } else { showToast("卫星视图关闭"); mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); isViewcSwitch = !isViewcSwitch;
}
|
缩放
1 2 3 4 5 6 7 8 9 10
| 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
1 2 3 4 5 6
| mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.ic_map_location); mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker, getMyColor(R.color.mapColor), getMyColor(R.color.mapColor)));
|
源码下载