android 百度地图3.0定位,Android 百度地圖 SDK v3.0.0 (二) 定位與結合方向傳感器...
新功能添加:
第一,定位功能;第二,與方向傳感器結合,通過旋轉手機進行道路的方向確認。
1、初次啟動定位
/**
*?定位的客戶端
*/
privateLocationClient?mLocationClient;
/**
*?定位的監聽器
*/
publicMyLocationListener?mMyLocationListener;
/**
*?當前定位的模式
*/
privateLocationMode?mCurrentMode?=?LocationMode.NORMAL;
/***
*?是否是第一次定位
*/
privatevolatilebooleanisFristLocation?=true;
/**
*?初始化定位相關代碼
*/
privatevoidinitMyLocation()
{
//?定位初始化
mLocationClient?=?newLocationClient(this);
mMyLocationListener?=?newMyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
//?設置定位的相關配置
LocationClientOption?option?=?newLocationClientOption();
option.setOpenGps(true);//?打開gps
option.setCoorType("bd09ll");//?設置坐標類型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
}
然后是定位的監聽器MyLocationListener:
/**
*?實現實位回調監聽
*/
publicclassMyLocationListenerimplementsBDLocationListener
{
@Override
publicvoidonReceiveLocation(BDLocation?location)
{
//?map?view?銷毀后不在處理新接收的位置
if(location?==null||?mMapView?==null)
return;
//?構造定位數據
MyLocationData?locData?=?newMyLocationData.Builder()
.accuracy(location.getRadius())
//?此處設置開發者獲取到的方向信息,順時針0-360
.direction(mXDirection).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mCurrentAccracy?=?location.getRadius();
//?設置定位數據
mBaiduMap.setMyLocationData(locData);
mCurrentLantitude?=?location.getLatitude();
mCurrentLongitude?=?location.getLongitude();
//?設置自定義圖標
BitmapDescriptor?mCurrentMarker?=?BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
MyLocationConfigeration?config?=?newMyLocationConfigeration(
mCurrentMode,?true,?mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);
//?第一次定位時,將地圖位置移動到當前位置
if(isFristLocation)
{
isFristLocation?=?false;
LatLng?ll?=?newLatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate?u?=?MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
}
可以看到,我們初始化了定位的參數,設置了定位的監聽器,每隔1s會進行一次定位,應用打開時,第一定位,會把地圖中心設置當前用戶位置。
定位也是比較耗電的,所以我們在onStart中開啟定位,在onStop中關閉定位~~這樣應用最小化時就不會一直在哪GPS請求定位了,用戶要是看你app一直在那定位,估計馬上就被卸載了~
@Override
protectedvoidonStart()
{
//?開啟圖層定位
mBaiduMap.setMyLocationEnabled(true);
if(!mLocationClient.isStarted())
{
mLocationClient.start();
}
//?開啟方向傳感器
myOrientationListener.start();
super.onStart();
}
@Override
protectedvoidonStop()
{
//?關閉圖層定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
//?關閉方向傳感器
myOrientationListener.stop();
super.onStop();
}
上面的傳感器的代碼,一會就會介紹~
記得在AndroidManifest.xml配一個service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote">
現在基本的定位功能已經實現了~不過我們還需要添加點擊定位按鈕和方向傳感器
2、我的位置
點擊我的位置菜單會調用center2myLoc方法。
caseR.id.id_menu_map_myLoc:
center2myLoc();
break;
/**
*?地圖移動到我的位置,此處可以重新發定位請求,然后定位;
*?直接拿最近一次經緯度,如果長時間沒有定位成功,可能會顯示效果不好
*/
privatevoidcenter2myLoc()
{
LatLng?ll?=?newLatLng(mCurrentLantitude,?mCurrentLongitude);
MapStatusUpdate?u?=?MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
很簡單,我們在定位的監聽器中已經保存了最近一次的定位經緯度,所以只需要點擊時,把地圖移動到相應的位置即可。
3、集成方向傳感器
首先是封裝的方向傳感器的類MyOrientationListener.java
packagecom.zhy.zhy_baidu_ditu_demo00;
importandroid.content.Context;
importandroid.hardware.Sensor;
importandroid.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
importandroid.hardware.SensorManager;
publicclassMyOrientationListenerimplementsSensorEventListener
{
privateContext?context;
privateSensorManager?sensorManager;
privateSensor?sensor;
privatefloatlastX?;
privateOnOrientationListener?onOrientationListener?;
publicMyOrientationListener(Context?context)
{
this.context?=?context;
}
//?開始
publicvoidstart()
{
//?獲得傳感器管理器
sensorManager?=?(SensorManager)?context
.getSystemService(Context.SENSOR_SERVICE);
if(sensorManager?!=null)
{
//?獲得方向傳感器
sensor?=?sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
//?注冊
if(sensor?!=null)
{//SensorManager.SENSOR_DELAY_UI
sensorManager.registerListener(this,?sensor,
SensorManager.SENSOR_DELAY_UI);
}
}
//?停止檢測
publicvoidstop()
{
sensorManager.unregisterListener(this);
}
@Override
publicvoidonAccuracyChanged(Sensor?sensor,intaccuracy)
{
}
@Override
publicvoidonSensorChanged(SensorEvent?event)
{
//?接受方向感應器的類型
if(event.sensor.getType()?==?Sensor.TYPE_ORIENTATION)
{
//?這里我們可以得到數據,然后根據需要來處理
floatx?=?event.values[SensorManager.DATA_X];
if(?Math.abs(x-?lastX)?>1.0)
{
onOrientationListener.onOrientationChanged(x);
}
//????????????Log.e("DATA_X",?x+"");
lastX?=?x?;
}
}
publicvoidsetOnOrientationListener(OnOrientationListener?onOrientationListener)
{
this.onOrientationListener?=?onOrientationListener?;
}
publicinterfaceOnOrientationListener
{
voidonOrientationChanged(floatx);
}
}
在onCreate中初始化方向傳感器
/**
*?初始化方向傳感器
*/
privatevoidinitOritationListener()
{
myOrientationListener?=?newMyOrientationListener(
getApplicationContext());
myOrientationListener
.setOnOrientationListener(newOnOrientationListener()
{
@Override
publicvoidonOrientationChanged(floatx)
{
mXDirection?=?(int)?x;
//?構造定位數據
MyLocationData?locData?=?newMyLocationData.Builder()
.accuracy(mCurrentAccracy)
//?此處設置開發者獲取到的方向信息,順時針0-360
.direction(mXDirection)
.latitude(mCurrentLantitude)
.longitude(mCurrentLongitude).build();
//?設置定位數據
mBaiduMap.setMyLocationData(locData);
//?設置自定義圖標
BitmapDescriptor?mCurrentMarker?=?BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
MyLocationConfigeration?config?=?newMyLocationConfigeration(
mCurrentMode,?true,?mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);
}
});
}
最后在onStart和onStop中分別開啟和關閉方向傳感器。
對於旋轉手機確定方向,實際上利用了
newMyLocationData.Builder()
//此處設置開發者獲取到的方向信息,順時針0-360????????????????????????????????????????????????????????????????????????????????????????????????.direction(mXDirection)
只需要把x方向的角度設置即可!
總結
以上是生活随笔為你收集整理的android 百度地图3.0定位,Android 百度地圖 SDK v3.0.0 (二) 定位與結合方向傳感器...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51单片机 驱动步进电机 C语言 lcd
- 下一篇: C++学习之路 | PTA乙级—— 10