1.显示地图
public class MainActivityextends AppCompatActivityimplements OnPermission {
public LocationClientmLocationClient =null;
private MyLocationListenermyListener =new MyLocationListener();
private MapViewmMapView =null;
private BitmapDescriptormCurrentMarker;
private BaiduMapmBaiduMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap =mMapView.getMap();
requestFilePermission();
}
private void requestFilePermission() {
XXPermissions.with(this)
.permission(Permission.Group.LOCATION)
.request(this);
}
@Override
public void hasPermission(List granted, boolean isAll) {
mLocationClient =new LocationClient(getApplicationContext());
//声明LocationClient类
mLocationClient.registerLocationListener(myListener);
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomBy(2);
mBaiduMap.animateMapStatus(mapStatusUpdate);
LocationClientOption option =new LocationClientOption();
option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系
// option.setIsNeedAddress(true);
////可选,是否需要地址信息,默认为不需要,即参数为false
//如果开发者需要获得当前点的地址信息,此处必须为true
mLocationClient.setLocOption(option);
mLocationClient.start();
mMapView.setLogoPosition(LogoPosition.logoPostionleftTop);
}
@Override
public void noPermission(List denied, boolean quick) {
Toast.makeText(MainActivity.this,"没有获取足够的权限",Toast.LENGTH_SHORT).show();
}
public class MyLocationListenerextends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location){
//此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果
//以下只列举部分获取地址相关的结果信息
//更多结果信息获取说明,请参照类参考中BDLocation类中的说明
String addr = location.getAddrStr(); //获取详细地址信息
String country = location.getCountry(); //获取国家
String province = location.getProvince(); //获取省份
String city = location.getCity(); //获取城市
String district = location.getDistrict(); //获取区县
String street = location.getStreet(); //获取街道信息
double latitude = location.getLatitude(); //获取纬度信息
double longitude = location.getLongitude(); //获取经度信息
String locationDescribe = location.getLocationDescribe(); //获取位置描述信息
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 构造定位数据
MyLocationData locData =new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// 设置定位数据
mBaiduMap.setMyLocationData(locData);
// 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);//定位图标
MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING
; //默认为 LocationMode.NORMAL 普通态
MyLocationConfiguration config =new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker); //开启方向
mBaiduMap.setMyLocationConfiguration(config);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
}
隐藏百度logo
View child =mMapView.getChildAt(1);
if (child !=null && (childinstanceof ImageView || childinstanceof ZoomControls)) {
child.setVisibility(View.INVISIBLE);
}
目前界面上是有缩放按钮的(+和-)
mMapView. showZoomControls(enable);设置隐藏
key监听
我们初始化sdk是在application里面配置好key,然后在application调用代码初始化
<application> android:name="baidumapsdk.demo.DemoApplication"
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发密钥" />
</application>
然后我们需要动态注册一个广播,看sdk初始化是否成功
public class SDKReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR))
{
// key 验证失败,相应处理 }
} }
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
mReceiver = new SDKReceiver();
registerReceiver(mReceiver, iFilter);
在ondestroy()
unregisterReceiver(mReceiver);
当用户在界面上疯狂操作之后,需要让用户回到自己定位的界面
dingwei.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LatLng point =new LatLng(myLatitude, myLongitude);//经纬度,可以通过BDLocation得到
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));
}
});