您的位置:首页 > 其它

百度地图定位当前并且加上方向传感器

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类、配置定位图层显示方式:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: