百度地图系列04——自定义Marker
来源:互联网 发布:软件架构设计 编辑:程序博客网 时间:2024/06/05 17:47
百度地图系列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、运行截图:
0 0
- 百度地图系列04——自定义Marker
- 百度地图,自定义Marker
- 百度地图——自定义覆盖物Marker
- 百度地图系列05——显示屏幕范围内的marker
- 百度地图系列之07——只显示圆内的marker+Fragment
- 百度地图点聚合自定义marker
- 百度地图+Marker纠偏
- 百度地图marker
- 百度地图添加自定义Marker+显示覆盖层次优先级
- Android 百度地图自定义marker 控件点击无效问题解决方法。
- 百度地图API自定义坐标marker点利用覆盖物框选后获取marker点信息
- 百度地图-自定义搜索、自定义marker、地图选址实用实例
- 百度地图Marker隐藏/显示
- 百度地图-marker 标 内容
- 百度地图植入android应用系列教程三实现百度地图上的覆盖物Marker与InfoWindow的使用
- 百度地图的添加覆盖物marker和InfoWindow总结,自定义标签
- Android 百度地图添加自定义marker(覆盖物)不显示图片的解决方法
- 百度地图周边检索功能的一些设置及自定义marker点
- Android Training - 适配不同的屏幕(Lesson 2 - 适配不同屏幕密度)
- 使用联邦(FEDERATION)访问远程的DB2数据库
- VC/MFC 使用jsoncpp解析json格式内容
- (一) 隔直电容分析
- 用foreach遍历集合和数组
- 百度地图系列04——自定义Marker
- 外挂
- (二) 一阶RC低通滤波器杂记
- Java web工程中获取系统精确时间(到秒级别)
- WaitForTargetFPS
- 给计算机系学生的建议
- 【Html】使用<pre>标签为你的网页加入大段代码
- #CocoaPods# 安装和常见问题
- RecyclerView介绍(五)----实现点击效果以及动画