上一篇部落格介紹了內建百度地圖的詳細步驟,這篇就記錄一下百度地圖的一些基本的設定,如果不知道怎麼內建的請檢視上一篇部落格內建的詳細步驟
地圖類型:
mMapView.setLogoPosition(LogoPosition.logoPostionleftTop);//設定百度地圖logo位置
mMapView.showZoomControls(false);//是否顯示縮放控件
mBaiduMap = mMapView.getMap();
//普通地圖
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//地圖類型
百度地圖SDK提供了3種預置的地圖類型(普通矢量地圖、衛星圖和空白地圖),BaiduMap 類提供圖層類型常量,如下:MAP_TYPE_NORMAL 普通地圖(包含3D地圖),MAP_TYPE_SATELLITE 衛星圖,MAP_TYPE_NONE
空白地圖;另外提供了2種常用圖層實時路況圖以及百度城市熱力圖。
交通圖:
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
//開啟交通圖
mBaiduMap.setTrafficEnabled(true);
熱力圖:
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
//開啟熱力圖
mBaiduMap.setBaiduHeatMapEnabled(true);
當地圖從20級放大到21級後,衛星圖、熱力圖、交通路況圖層将不再顯示;縮小到20級後,衛星圖、熱力圖、交通路況圖層,會再正常顯示。
定位:
/**
* 初始化定位
*/
private void initLocation(){
// 開啟定位圖層
mBaiduMap.setMyLocationEnabled(true);
myListener = new MyLocationListener();
mLocationClient = new LocationClient(HistoryActivity.this);
mLocationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打開gps
option.setCoorType("bd09ll"); // 設定坐标類型
option.setScanSpan(30000);//取定位的時間間隔
mLocationClient.setLocOption(option);
mLocationClient.start();
}
定 位監聽
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(final BDLocation location) {
//擷取定位結果
// map view 銷毀後不在處理新接收的位置
if (location == null || mMapView == null) {
return;
}
mCurrentLat = location.getLatitude();
mCurrentLon = location.getLongitude();
mCurrentAccracy = location.getRadius();
locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設定開發者擷取到的方向資訊,順時針0-360
.direction(mCurrentDirection).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
// 設定定位圖層的配置(定位模式,是否允許方向資訊,使用者自定義定位圖示)
mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);
mBaiduMap.setMyLocationConfiguration();
LatLng ll = new LatLng(location.getLatitude(),//中心點的經緯度
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(12.0f);//設定中心的和縮放級别
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
定位模式
地圖SDK支援三種定位模式:NORMAL(普通态), FOLLOWING(跟随态), COMPASS(羅盤态)
mCurrentMode = LocationMode.FOLLOWING;//定位跟随态
mCurrentMode = LocationMode.NORMAL; //預設為 LocationMode.NORMAL 普通态
mCurrentMode = LocationMode.COMPASS; //定位羅盤态
自定義定位圖示
支援自定義定位圖示樣式,替換定位icon
mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.icon_geo);
自定義精度圈填充顔色
accuracyCircleFillColor = 0xAAFFFF88;//自定義精度圈填充顔色
自定義精度圈邊框顔色
accuracyCircleStrokeColor = 0xAA00FF00;//自定義精度圈邊框顔色