需求
功能需求
- 使用百度地图+定位SDK
- 显示当前的位置坐标,即维度+经度+地址
项目环境
具体操作步骤
注册百度地图开发者
在如下网址操作,注册账号 百度地图开放平台
获取开发秘钥
-
进入控制台 -
创建应用 -
设置应用名称,选择启用服务 -
获取发布版SHA1,包名
获取项目SHA1与包名
- 打开自己的项目获取包名
- 找到.android文件夹下的debug.keystroe
- 在当前文件夹打开控制台(cmd)输入如下代码
- 输入默认密码android,查看SHA1
- 填入即可
下载Jar包
点击下载开发包
- 选择如下两个模块
将Jar包添加至项目
- 在app目录下的build.gradle文件中android块中配置sourceSets标签
sourceSets {
main {
jniLibs.srcDir 'libs'
}
}
- 解压文件夹至项目libs
- 右键BaiduLBS_Android.jar选择Add As Library
- 可以发现项目依赖中已添加此包
显示地图环境配置
- 在<application>中加入如下代码配置开发密钥(AK)
这个密钥就是之前申请的密钥
<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者 key" />
</application>
- 在<application/>外部添加如下权限声明
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
使用定位环境配置
- 在AndroidManifest.xml加入如下权限使用声明
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- 在Application标签中声明定位的service组件
<service android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote"/>
显示地图并且显示位置坐标
编写思路
- 使用哪个页面显示,这里我使用的是在Fragment里面显示
- 使用MapView
- 使用LocationClient
页面格式
<?xml version="1.0" encoding="utf-8"?>
<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=".BottonSet">
<LinearLayout
android:id="@+id/L5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e0000000"
android:orientation="vertical">
<TextView
android:id="@+id/Lat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="维度:"
android:textColor="@color/white"
android:textSize="15sp" />
<TextView
android:id="@+id/Lon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="经度:"
android:textColor="@color/white"
android:textSize="15sp" />
<TextView
android:id="@+id/Addr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="地址:"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
效果如下
Fragmaent所使用的变量
private View view;
private MapView mMapView = null;
private BaiduMap mBaiduMap = null;
private LocationClient mLocationClient;
private boolean isFirstLocation = true;
private TextView tv_Lat;
private TextView tv_Lon;
private TextView tv_Addr;
重写四个方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_botton_set, container, false);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
SDKInitializer.initialize(getActivity().getApplicationContext());
SDKInitializer.setCoordType(CoordType.BD09LL);
requestLocation();
LinearLayout l5 = view.findViewById(R.id.L5);
l5.addView(mMapView);
return view;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mLocationClient.stop();
mMapView.onDestroy();
}
初始化 requestLocation()
private void requestLocation() {
initLocation();
mLocationClient.start();
}
初始化定位 initLocation();
private void initLocation() {
mLocationClient = new LocationClient(getActivity().getApplicationContext());
mMapView = new MapView(getActivity().getApplicationContext());
mBaiduMap = mMapView.getMap();
tv_Lat = view.findViewById(R.id.Lat);
tv_Lon = view.findViewById(R.id.Lon);
tv_Addr = view.findViewById(R.id.Addr);
mBaiduMap.setMyLocationEnabled(true);
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");
option.setScanSpan(1000);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(new MyLocationListener());
}
自定义定位监听器 MyLocationListener
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
if (bdLocation == null || mMapView == null) {
return;
}
tv_Lat.setText("维度:" + bdLocation.getLatitude() + "");
tv_Lon.setText("经度:" + bdLocation.getLongitude() + "");
tv_Addr.setText("位置:" + bdLocation.getAddrStr());
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation || bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
navigateTo(bdLocation);
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(bdLocation.getRadius())
.direction(bdLocation.getDirection()).latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
}
}
处理第一次定位 navigateTo()
private void navigateTo(BDLocation bdLocation) {
if (isFirstLocation) {
LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(update);
isFirstLocation = false;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(bdLocation.getRadius())
.direction(bdLocation.getDirection()).latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
}
}
测试显示结果
仓库地址
https://gitee.com/lazy_lazy_tang_lan/as-coding-studying
|