mLocationClient.start();
}
// 开启方向传感器
myOrientationListener.start();
super.onStart();
}
@Override
protected void onStop()
{
// 关闭图层定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
// 关闭方向传感器
myOrientationListener.stop();
super.onStop();
}
上面的传感器的代码,一会就会介绍~
记得在AndroidManifest.xml配一个service
<service
android:name=“com.baidu.location.f”
android:enabled=“true”
android:process=“:remote” >
现在基本的定位功能已经实现了~不过我们还需要添加点击定位按钮和方向传感器
2、我的位置
点击我的位置菜单会调用center2myLoc方法。
case R.id.id_menu_map_myLoc:
center2myLoc();
break;
/**
-
地图移动到我的位置,此处可以重新发定位请求,然后定位;
-
直接拿最近一次经纬度,如果长时间没有定位成功,可能会显示效果不好
*/
private void center2myLoc()
{
LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude);
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
很简单,我们在定位的监听器中已经保存了最近一次的定位经纬度,所以只需要点击时,把地图移动到相应的位置即可。
3、集成方向传感器
首先是封装的方向传感器的类MyOrientationListener.java
package com.zhy.zhy_baidu_ditu_demo00;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class MyOrientationListener implements SensorEventListener
{
private Context context;
private SensorManager sensorManager;
private Sensor sensor;
private float lastX ;
private OnOrientationListener onOrientationListener ;
public MyOrientationListener(Context context)
{
this.context = context;
}
// 开始
public void start()
{
// 获得传感器管理器
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);
}
}
// 停止检测
public void stop()
{
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
@Override
public void onSensorChanged(SensorEvent event)
{
// 接受方向感应器的类型
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
// 这里我们可以得到数据,然后根据需要来处理
float x = event.values[SensorManager.DATA_X];
if( Math.abs(x- lastX) > 1.0 )
{
onOrientationListener.onOrientationChanged(x);
}
// Log.e(“DATA_X”, x+“”);
lastX = x ;
}
}
public void setOnOrientationListener(OnOrientationListener onOrientationListener)
{
this.onOrientationListener = onOrientationListener ;
}
public interface OnOrientationListener
{
void onOrientationChanged(float x);
}
}
在onCreate中初始化方向传感器
/**
- 初始化方向传感器
*/
private void initOritationListener()
{
myOrientationListener = new MyOrientationListener(
getApplicationContext());
myOrientationListener
.setOnOrientationListener(new OnOrientationListener()
{
@Override
public void onOrientationChanged(float x)
{
mXDirection = (int) x;
// 构造定位数据
MyLocationData locData = new MyLocationData.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 = new MyLocationConfigeration(
mCurrentMode, true, mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);
}
});
}
最后在onStart和onStop中分别开启和关闭方向传感器。
对于旋转手机确定方向,实际上利用了
new MyLocationData.Builder()
//此处设置开发者获取到的方向信息,顺时针0-360 .direction(mXDirection)
只需要把x方向的角度设置即可~是不是很简单~
好了,介绍完毕了,关闭地图样式的切换,以及跟随、罗盘等模式的切换就不介绍了,大家自己看下源码~~
注:开发者key需要换成自己申请的,不清楚申请的请看第一篇博客的。
百度地图相关博客视频版本已经上线:Android中百度地图的使用期待您的支持。
博主部分视频已经上线,如果你不喜欢枯燥的文本,请猛戳(初录,期待您的支持):
2、Android自定义控件实战 打造Android流式布局和热门标签
百度地图相关博客视频版本已经上线:Android中百度地图的使用期待您的支持。
博主部分视频已经上线,如果你不喜欢枯燥的文本,请猛戳(初录,期待您的支持):