百度地图定位当前并且加上方向传感器
2015-08-10 15:50
483 查看
今天继续分享百度地图的定位到当前的功能
1.AndroidManiFest.xml:
在这边一定要记得注册一个service,不然是跑步起来的。
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
2.LocationActivity.class:
public class LocationMapActivtiy extends Activity {
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private LocationMode mCurrentMode;
BitmapDescriptor mCurrentMarker;
MapView mMapView;
BaiduMap mBaiduMap;
boolean isFirstLoc = true;// 是否首次定位
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mCurrentMode = LocationMode.NORMAL;
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null)
return;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
}
注:MyLocationConfiguration类、配置定位图层显示方式:
1.AndroidManiFest.xml:
在这边一定要记得注册一个service,不然是跑步起来的。
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
2.LocationActivity.class:
public class LocationMapActivtiy extends Activity {
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private LocationMode mCurrentMode;
BitmapDescriptor mCurrentMarker;
MapView mMapView;
BaiduMap mBaiduMap;
boolean isFirstLoc = true;// 是否首次定位
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mCurrentMode = LocationMode.NORMAL;
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null)
return;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
}
注:MyLocationConfiguration类、配置定位图层显示方式:
相关文章推荐
- 大讲台谈hive(后篇二)
- A1044 Shopping in Mars (25)
- 学习设计模式——单例模式
- 使用Material Design Tint和视图详解
- oracle函数学习(sign和trunc)
- Android添加View作为横线、竖线
- 关于真机测试
- 使用Material Design Tint和视图详解
- Linux命令详解:alias创建自己的命令
- Webform Lable
- Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)
- poj1386 字符串类型的一笔画问题
- jquery的动画函数animate()讲解一
- NOIP2013货车运输 (最大生成树,分块求LCA)
- Walking Ant 1671 (广搜)
- c#处理json
- Python实现短网址ShortUrl的Hash运算实例讲解
- spring的AOP和事务
- linux查找技巧: find grep xargs
- (4.2.17)圆形菜单CircularFloatingActionMenu