<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dip"
android:background="#D000"
android:minWidth="100dip"
android:orientation="vertical"
android:padding="2dp" >
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="定位icon" >
<RadioButton
android:id="@+id/defaulticon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="默认图标" >
</RadioButton>
<RadioButton
android:id="@+id/customicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义图标" >
</RadioButton>
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_marginTop="10dip" />
</RelativeLayout>
/**
* 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
* 介绍定位图层的基本方法
*
*/
public class LocationDemo extends Activity {
//定位服务的客户端。宿主程序在客户端声明此类,并调用
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
/**
* 定位模式 分为高精度定位模式 低功耗定位模式 仅设备定位模式
* 高精度定位模式:这种定位模式下,会同时使用网络定位和GPS定位,优先返回最高精度的定位结果;
* 低功耗定位模式:这种定位模式下,不会使用GPS,只会使用网络定位(Wi-Fi和基站定位)
* 仅用设备定位模式:这种定位模式下,不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位
* */
private LocationMode mCurrentMode;
//bitmap 描述信息
BitmapDescriptor mCurrentMarker;
MapView mMapView;
BaiduMap mBaiduMap;
// UI相关
OnCheckedChangeListener radioButtonListener;
Button requestLocButton;
boolean isFirstLoc = true;// 是否首次定位
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
//模式按钮
requestLocButton = (Button) findViewById(R.id.button1);
mCurrentMode = LocationMode.NORMAL;
requestLocButton.setText("普通");
OnClickListener btnClickListener = new OnClickListener() {
public void onClick(View v) {
switch (mCurrentMode) {
//普通
case NORMAL:
requestLocButton.setText("跟随");
mCurrentMode = LocationMode.FOLLOWING;
/**
* setMyLocationConfigeration(MyLocationConfiguration configeration):
* 设置定位图层配置信息,只有先允许定位图层后设置定位图层配置信息才会生效,参见 setMyLocationEnabled(boolean)
* */
/**
* MyLocationConfiguration:配置定位图层显示方式
* MyLocationConfiguration(MyLocationConfiguration.LocationMode mode,
* boolean enableDirection, BitmapDescriptor customMarker):
*
* locationMode:定位图层显示方式
* enableDirection:是否允许显示方向信息
* customMarker:用户自定义定位图标
* */
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
//罗盘
case COMPASS:
requestLocButton.setText("普通");
mCurrentMode = LocationMode.NORMAL;
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
//跟随
case FOLLOWING:
requestLocButton.setText("罗盘");
mCurrentMode = LocationMode.COMPASS;
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
}
}
};
requestLocButton.setOnClickListener(btnClickListener);
RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup);
radioButtonListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.defaulticon) {
// 传入null则,恢复默认图标
mCurrentMarker = null;
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, null));
}
if (checkedId == R.id.customicon) {
// 修改为自定义marker
/**
* BitmapDescriptorFactory:bitmap 描述信息工厂类,在使用该类方法之前请确保已经调用了
* SDKInitializer.initialize(Context) 函数以提供全局 Context 信息。
*
* fromResource(int resourceId):根据资源 Id 创建 bitmap 描述信息
* */
mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
}
}
};
group.setOnCheckedChangeListener(radioButtonListener);
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
/**
*setMyLocationEnabled(boolean enabled):设置是否允许定位图层
* */
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
/**
* registerLocationListener(BDLocationListener listener) :
* 注册定位监听函数
* */
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
/**
*setOpenGps(boolean openGps) :是否打开gps进行定位
* */
option.setOpenGps(true);
/**
* setCoorType(java.lang.String coorType) :设置坐标类型
* */
option.setCoorType("bd09ll");
/**
*setScanSpan(int scanSpan) : 设置扫描间隔,单位是毫秒
* */
option.setScanSpan(1000);
/**
*setLocOption(LocationClientOption locOption) : 设置 LocationClientOption
* */
mLocClient.setLocOption(option);
/**
*start():启动定位sdk
* */
mLocClient.start();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
/**
*onReceiveLocation(BDLocation location):定位请求回调函数
* */
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null){
return;
}
/**
*MyLocationData:定位数据
*MyLocationData.Builder:定位数据建造器
*accuracy:定位精度
*direction:GPS定位时方向角度
*latitude:百度纬度坐标
*longitude:百度经度坐标
* */
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100)
.latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
/**
*setMyLocationData(MyLocationData data):
* 设置定位数据, 只有先允许定位图层后设置数据才会生效,参见 setMyLocationEnabled(boolean)
* */
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
/**
*LatLng:地理坐标基本数据结构
*BDLocation.getLatitude() : 获取纬度坐标
*BDLocation.getLongitude():获取经度坐标
* */
LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
/**
*MapStatusUpdateFactory.newLatLng(LatLng latLng):设置地图新中心点
* */
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();
}
}