功能说明
调用百度地图SDK,实现开启位置信息且联网后室内实时定位(经纬度+地址信息),并通过switch控件实现普通和卫星地图的切换。
关键源码
XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity">
<!--百度地图控件-->
<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="fill_parent"
android:layout_height="wrap_content"
android:background="#e0000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="纬度:"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_Lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经度:"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_Lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="地址:"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
JAVA
根据提词器import相关文件
public class MapActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
LocationClient mLocationClient; //定位客户端
MapView mapView; //Android Widget地图控件
BaiduMap baiduMap;
boolean isFirstLocate = true;
TextView tv_Lat; //纬度
TextView tv_Lon; //经度
TextView tv_Add; //地址
Switch swtch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_map);
swtch = findViewById(R.id.swtch);
swtch.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) this);
//隐藏状态栏
Objects.requireNonNull(getSupportActionBar()).hide();
//如果没有定位权限,动态请求用户允许使用该权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else {
requestLocation();
}
}
//地图模式切换
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
String state = compoundButton.isChecked() ? "Satellite" :"Normal";
Toast.makeText(this,"MODE:"+state,Toast.LENGTH_SHORT).show();
if(compoundButton.isChecked()){
baiduMap.setMapType(MAP_TYPE_SATELLITE);
}else{
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 1) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "没有定位权限!", Toast.LENGTH_LONG).show();
finish();
} else {
requestLocation();
}
}
}
private void requestLocation() {
initLocation();
mLocationClient.start();
}
//初始化
private void initLocation() {
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
mapView = findViewById(R.id.bmapView);
baiduMap = mapView.getMap();
tv_Lat = findViewById(R.id.tv_Lat);
tv_Lon = findViewById(R.id.tv_Lon);
tv_Add = findViewById(R.id.tv_Add);
LocationClientOption option = new LocationClientOption();
//设置扫描时间间隔
option.setScanSpan(1000);
//设置定位模式,三选一
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
/*option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
*option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);*/
//设置需要地址信息
option.setIsNeedAddress(true);
//保存定位参数
mLocationClient.setLocOption(option);
}
//内部类,百度位置监听器
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
tv_Lat.setText(bdLocation.getLatitude()+"");
tv_Lon.setText(bdLocation.getLongitude()+"");
tv_Add.setText(bdLocation.getAddrStr());
if(bdLocation.getLocType()==BDLocation.TypeGpsLocation || bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
navigateTo(bdLocation);
}
}
}
private void navigateTo(BDLocation bdLocation) {
if(isFirstLocate){
LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
isFirstLocate = false;
}
}
//生命周期
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
mapView.onDestroy();
}
}
功能测试
核心技术
阅读官方文档,调用API:
百度地图开发平台登录百度账号后,在API控制台点击“创建应用”申请开发密钥AK:
- SHA1
- 进入cmd控制台,执行”cd .android”定位到”.android”文件夹下。
- 执行“keytool -list -v -keystore debug.keystore”,调试版本默认密码是: android,此时可在控制台显示的信息中获取SHA1值。
- 程序包名
下载SDK本地依赖
-
下载开发包(包括基础定位和基础地图) - 开发包格式:JAR - 应用发布平台:标准
-
将解压后开发包(jar文件和so文件)拷贝至工程视图的libs文件夹下 - 在app目录下的build.gradle文件中android块中配置sourceSets标签:
sourceSets { main { jniLibs.srcDir 'libs' } } -
在app目录下的build.gradle文件中dependencies块中添加:
implementation files('libs\BaiduLBS_Android.jar')
AndroidManifest.xml
-
application块外添加权限:
<!--百度定位所需要权限,前面是LOCATE权限组的2个危险权限--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!--百度定位所需要的普通权限--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <!--因为程序要与百度云服务交互--> <uses-permission android:name="android.permission.INTERNET"/> ``` -
application块中添加:
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="此处为之前申请的开发密钥AK" /><service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" />
心得体会
- 初次使用debug.keystore需填上默认信息
Keystore name: "debug.keystore"
Keystore password: "android"
Key alias: "androiddebugkey"
Key password: "android"
-
模拟器测试定位为虚拟位置,需要真机测试才能获取实际位置。
-
百度地图SDK提供了3种类型的地图资源(普通矢量地图、卫星图和空白地图)
类型名称 说明 MAP_TYPE_NORMAL 普通地图(包含3D地图) MAP_TYPE_SATELLITE 卫星图 MAP_TYPE_NONE 空白地图 -
AS默认文件视图是Android视图,整合了一些最常用的文件夹(隐藏了不常用的文件夹),而Project视图则是项目真实的文件结构,在进行某些配置的时候需要切换到Project视图。