BaiduMap基礎

BaiduMap baiduMap;
MapView mapView=(MapView)findViewById(R.id.mapView);
baiduMap=mapView.getMap();


/**
*一、設置地圖類型(NORMAL、SATELLITE)
*/
//普通地圖
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
//衛星地圖
baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);

//開啓實時交通圖(zIndex=5)
baiduMap.setTrafficEnabled(true);
//開啓城市熱力圖(zIndex=6)
baiduMap.setBaiduHeatMapEnabled(true);


/**
*二、MarkerOption標註指定位置(標註圖層Marker,zIndex=9)
*/


LatLng ll=new LatLng(latitude,longtitude);
BitmapDescriptor bitmap=BitmapDescriptorFactory.fromResource(R.drawable.bitmap);


//----------MarkerOptions---------
OverlayOptions options=new MarkerOptions()
.position(ll)       //圖標位置
.icon(bitmap)       //圖標圖片
.draggable(true)    //可以被拖動
.zIndex(9);
//!!!添加Marker圖層!!!
baiduMap.addOverlay(options);


//其他覆蓋物OverlayOptions
文字覆蓋物TextOptions   (zIndex=9)
幾何圖形覆蓋物PolygonOptions  (zIndex=8)
地形圖圖層GroundOverlayOptions  (zIndex=3)


//彈出窗覆蓋物InfoWindow   (zIndex=12)
//檢索結果覆蓋物PoiOverlay   (zIndex=7)
//公交路線規劃TransitRouteOverlay




/**
*三、定位相關


*1、定義定位客戶端和定位監聽器
*LocationClient locationClient;
*MyLocationListener myLocationListener;


*2、註冊監聽器
*locationClient.registerLocationListener(myLocationListener);


*3、包括設置定位參數(LocationClientOption)
*.setCoorType("bd0911")--座標類型(百度經緯度)
*.setScanSpan(1000)--定位時間間隔(ms)
*.setOpenGps(true)--打開GPS
*.setIsNeedAddress(true)--是否需要位置信息


*4、定位監聽(MyLocationListener implements BDLocationListener)
*【獲取定位數據MyLocationData】
*設置經緯度、精度accuracy


*【定位圖層顯示方式MyLocationConfiguration】
*第一個參數:定位模式(普通、羅盤、跟隨)
*第二個參數:是否顯示方向
*第三個參數:設置定位圖標(BitmapDescriptor)
*MyLocationConfiguration(LocationMode.NORMAL,true,bitmap);


*【當前位置更新顯示在中心位置MapStatusUpdate】
*第一個參數:定位中心點位置,縮放比例(3-19)
*MapStatusUpdate update=MapStatusUpdateFactory.newLatLngZoom(ll,17);
*baiduMap.animateMapStatus(update);    //更新地圖
*
*/


//定位客戶端LocationClient
LocationClient locationClient;
//定位監聽器MyLocationListener
MyLocationListener myLocationListener;


//實例化
locationClient=new LocationClient();
myLocationListener=new MyLocationListener();


//!!!註冊定位監聽器
locationClient.registerLocationListener(myLocationListener);


//---------設置定位客戶端參數LocationClientOption-------------
LocationClientOption option=new LocationClientOption();
option.setScanSpan(1000);   //定位間隔時間
option.setCoorType("bd0911");   //座標類型(bd0911爲百度經緯度)
option.setOpenGps(true);    //打開GPS
option.setIsNeedAdress(true);   //是否需要位置信息
//!!!
locationClient.setLocOption(option);


//-----------監聽器的類MyLocationListener------------
public class MyLoctionListener implements BDLocationListener
{

//重寫onReceiveLocation方法
@Override
public void onReceiveLocation(BDLocation location)
{
//------------獲取定位數據MyLocationData--------------
MyLocationData locData=new MyLocationData.Builder()
.accuracy(location.getRadius())
.laititude(location.getLatitude())
.longtitude(location.getLongtitude())
.builder();
//!!!設置定位數據!!!
baiduMap.setMyLocationData(locData);

//---------配置定位圖層顯示方式MyLocationConfiguration-----------
BitmapDescriptor bitmap=BitmapDescriptorFactory.fronResource(R.drawable.image);
//第一個參數:定位模式(普通、羅盤、跟隨);第二個參數:是否顯示方向;第三個參數:定位圖標
MyLocationConfiguration config=new MyLocationConfiguration(LocationMode.NORMAL,true,bitmap);
//!!!設置定位圖層顯示方式!!!
baiduMap.setMyLocationConfigeration(config);

//----------更新地圖MapStatusUpdate,定位到當前位置,當前位置處於中心,設置縮放比例--------------
if(isFirstLoc){
//第一次定位時更新
isFirstLoc=false;
LatLng ll=new LatLng(location.getLatitude(),location.getLongtitude());
float f=baiduMap.getMaxZoomLevel();   //獲取最大縮放比例
//第一個參數爲中心點座標;第二個參數爲縮放比例
MapSatusUpdate update=MapStatusUpdateFactory.newLatLngZoom(ll,f-2);
//!!!更新地圖!!!
baiduMap.animateMapStatus(update);
}
}
}




/**
*四、結合傳感器,設置定位時的方向(direction)


*1、創建一個MyOrientationListener類,繼承SensorEventListener


*2、獲取傳感器服務、註冊傳感器
//獲取傳感器服務getSystenService
*SensorManager sensorManager = (SensorManager)mContext.getSystenService(Context.SENSOR_SERVICE);
//獲取傳感器類型getDefaultSensor
*Sensor sensor = SensorManager.getDefaultSensor(Sensor.ORIENTATION);
//註冊傳感器(第一個參數:SensorEventListener;第二個參數:Sensor;第三個參數:Rate(獲取傳感器的速率))
*sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_UI);


*3、定義OnOrientationListener接口,接口裏實現setOnOrientationChanged方法
定義setOnOrientationListener(OnOrientationListener onOrientationListener)方法


*4、重寫onSensorChanged方法
*public void onSensorChanged(SensorEvent event)
//獲取傳感器x位置的值
*float x=event.values[SensorManager.DATA_X];
//調用OnOrientationListener接口中的setOnOrientationChanged方法,傳遞x位置進去
*onOrientationListener.setOnOrientationChanged(x);


*5、在主函數中定義MyOrientationListener類
*MyOrientationListener myOrientationListener;
//調用setOnOrientationListener方法,new 一個OnOrientationListener接口
*myOrientationListener.setOnOrientationListener(new OnOrientationListener()
*重寫setOnOrientationChanged方法,將w位置信息傳遞過來
*
*/


public class MyOrientationListener implements SensorEventListener
{
//定義傳感器服務器
SensorManager sensorManager;
//定義傳感器類型
Sensor sensor;
//獲取OnOrientationListener接口
private OnOrientationListener onOrientationListener;

//定義方向x位置的值
float lastX=0.0;

//定義上下文
private Context mContext;
//定義自身方法,傳遞上下文參數
public MyOrientationListener(Context context)
{
this.mContext=context;
}

//---------------在onStart方法中獲取傳感器和註冊監聽器------------------
public void onStart()
{
//獲取傳感器服務
sensorManage=(SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
//獲取傳感器類型--Orientation方向傳感器
sensor=SensorManager.getDefaultSensor(Sensor.ORIENTATION);
//註冊監聽器(第一個參數:SensorEventListener監聽傳感器事件的監聽器;第二個參數:Sensor傳感器類型;第三個參數:Rate獲取傳感器數據的頻率)
sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_UI);
}


public void onStop()
{
//取消註冊監聽器
sensorManager.unregisterListener(this);
}


//------------重寫onSensorChanged方法,獲取x位置變化信息-----------------------
@Override
public void onSensorChanged(SensorEvent event)
{
//獲取傳感器x位置的值(即方向信息)
float x=event.values[SensorManager.DATA_X];
//如果倆次x的位置大於1.0了,更新位置
if(Math.abs(x-lastX)>1.0)
{
//調用OnOrientationListener接口的setOnOrientationChanged方法
onOrientationListener.setOnOrientationChanged(x);
}
lastX=x;
}

//定義setOnOrientationListener方法,傳遞onOrientationListener接口
public void setOnOrientationListener(OnOrientationListener onOrientationListener)
{
this.onOrientationListener=onOrientationListener;
}

//定義OnOrientationListener接口
public interface OnOrientationListener
{
//定義一個方法,傳遞傳感器x參數
void setOnOrientationChanged(float x);
}
}




//-----------------在主函數調用這個類----------------
private MyOrientationListener myOrientationLitener;
//定義x位置變化值
private mCurrentX;


//實例化MyOrientationListener對象,將應用的上下文傳遞過去
myOrientationListener=new MyOrientationListener(getApplicationContext());
//調用MyOrientationListener中的setOnOrientationListener方法
//(new 一個新的OnOrientationListener接口,重寫該接口裏的setOnOrientationChanged方法)
myOrientationListener.setOnOrientationListener(new OnOrientationListener())
{
@Override
public void setOnOrientationChanged(float x)
{
mCurrentX=x;
//獲取定位數據MyLocationData,將x位置加進去
MyLocationData locData=new MyLocationData.Builder()
.direction(mCurrentX)    //設置方向信息
.latitude(latitude)
.longtitude(longtitude)
.accuracy(mCurrentAccuracy)
.build();
baiduMap.setMyLocationData(locData);
}
}


onStart()
{
//設置定位圖層可見
baiduMap.setMyLocationEnable(true);
//調用MyOrientationListener的onStart方法,獲取傳感器服務和註冊傳感器
myOrientationListener.onStart();
}


onStop()
{
//設置定位圖層不可見
baiduMap.setMyLocationEnable(false);
//調用MyOrientationListener的onStop方法,取消註冊傳感器
myOrientationListener.onStop();
}




/**
*五、路線BaiduMapRoutePlan
*
*1、設置路線起始位置RouteParaOption
*
*2、打開百度地圖路線
*
*/


//設置路線起始位置RouteParaOption
RouteParaOption option=new RouteParaOption()
.startPoint(l1)
.endPoint(l2);


try{
//打開百度地圖路線(三種路線選擇:WalkingRout、TransitRout、DrivingRoute)
BaiduMapRoutePlan.openBaiduMapDrivingRoute(option,MainActivity.this);
}
//異常處理,手機上沒有百度地圖客戶端時
catch(Exception e){
e.printStackTrace();
//調用showDialog方法提示
showDialog();
}



/**
*六、未下載百度地圖對話框showDialog提醒下載OpenClientUtil
*/


AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("");
builder.setMessage("");
builder.setPositiveButton("確定",new OnClickListener(){
@Override 
public void onClick(DialogInterface dialog,int i)
{
//點擊後對話框消失
dialog.dismiss();
//利用OpenCilentUtil調用百度客戶端工具類的getLatestBaiduMapApp方法,獲取下載百度地圖的網址
OpenClientUtil.getLatestBaiduMapApp(MainActivity.this);
}
});

builder.create().show();


項目代碼:

https://github.com/yxStory/BaiduMap


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章