百度地图系列04——自定义Marker
本文主要写的是android百度地图开发时自定义marker.上一章百度地图系列03——简单地图定位
1、首先导入BaiduLBS_Android.jar包,还有与之相关的so包,可在百度api上下载。
activity_mylocation.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.baidu.mapapi.map.MapView
android:id="@+id/mylocate_mv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
/>
</LinearLayout>
2、MainActivity代码部分:
public class MainActivity extends Activity {
private BaiduMap mBaiduMap;
private MapView mapView;
//定位客户端
private LocationClient mLocationClient;
//定位监听器
private MyLocationListener myLocationListener;
//定位模式
private LocationMode mLocationMode = LocationMode.NORMAL;
//是否是第一次定位
private volatile boolean isFirstLocation = true;
//最新一次经纬度
private double mCurrentLantitude;
private double mCurrentLongitude;
//当前精度
private float mCurrentAccracy;
//方向传感器的监听器
private MyOrientationListener myOrientationListener;
/**
* 方向传感器X方向的值
*/
private int mXDirection;
/**
* 地图定位的模式
*/
// 初始化全局 bitmap 信息,不用时及时 recycle
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_mylocation);
// 第一次定位
isFirstLocation = true;
mapView = (MapView) findViewById(R.id.mylocate_mv);
mBaiduMap = mapView.getMap();
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);
//初始化定位
initMyLocation();
// 初始化传感器
initOritationListener();
}
/**
* 初始化方向传感器
*/
public 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);
MyLocationConfiguration config = new MyLocationConfiguration(
mLocationMode, true, mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);
}
});
}
public void initMyLocation()
{
// 定位初始化
mLocationClient = new LocationClient(this);
myLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(myLocationListener);
// 设置定位的相关配置
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener
{
@Override
public void onReceiveLocation(BDLocation location)
{
// map view 销毁后不在处理新接收的位置
if (location == null || mapView == null)
return;
// 构造定位数据
MyLocationData locData = new MyLocationData.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);
MyLocationConfiguration config = new MyLocationConfiguration(
mLocationMode, true, mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);
// 第一次定位时,将地图位置移动到当前位置
if (isFirstLocation)
{
isFirstLocation = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
}
@Override
protected void onStart()
{
// 开启图层定位
mBaiduMap.setMyLocationEnabled(true);
if (!mLocationClient.isStarted())
{
mLocationClient.start();
}
// 开启方向传感器
myOrientationListener.start();
super.onStart();
}
@Override
protected void onStop()
{
// 关闭图层定位
mBaiduMap.setMyLocationEnabled(false);
mLocationClient.stop();
// 关闭方向传感器
myOrientationListener.stop();
super.onStop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mapView.onDestroy();
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
mapView.onResume();
super.onResume();
}
}
3、运行截图: