作者在 2015-10-27 11:17:36 发布以下内容
public class MainActivity extends Activity {
private MapView mMapView;
private LocationClient locationClient;
private BaiduMap baiduMap;
private boolean firstLocation;
private BitmapDescriptor mCurrentMarker;
private MyLocationConfigeration config;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//此方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mMapView =(MapView)findViewById(R.id.baiDuMv);
baiduMap = mMapView.getMap();
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15f);
baiduMap.setMapStatus(msu);
// 定位初始化
locationClient = new LocationClient(this);
firstLocation =true;
// 设置定位的相关配置
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setOpenGps(true);
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
locationClient.setLocOption(option);
// 设置自定义图标
BitmapDescriptor myMarker = BitmapDescriptorFactory
.fromResource(R.drawable.navi_map);
MyLocationConfigeration config = new MyLocationConfigeration(
MyLocationConfigeration.LocationMode.FOLLOWING, true, myMarker);
locationClient.registerLocationListener(new 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();
// 设置定位数据
baiduMap.setMyLocationData(locData);
// 第一次定位时,将地图位置移动到当前位置
if (firstLocation)
{
firstLocation = false;
LatLng xy = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate status = MapStatusUpdateFactory.newLatLng(xy);
baiduMap.animateMapStatus(status);
}
}
});
}
@Override
protected void onStart()
{
// 如果要显示位置图标,必须先开启图层定位
baiduMap.setMyLocationEnabled(true);
if (!locationClient.isStarted())
{
locationClient.start();
}
super.onStart();
}
@Override
protected void onStop()
{
// 关闭图层定位
baiduMap.setMyLocationEnabled(false);
locationClient.stop();
super.onStop();
}
@Override
protected void onDestroy()
{
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy()
mMapView.onDestroy();
mMapView = null;
}
@Override
protected void onResume()
{
super.onResume();
// 在activity执行onResume时执行mMapView. onResume ()
mMapView.onResume();
}
@Override
protected void onPause()
{
super.onPause();
// 在activity执行onPause时执行mMapView. onPause ()
mMapView.onPause();
}
}
//http://www.cnblogs.com/jerehedu/p/4891658.html