目录
需求展示
效果展示:
申请百度地图API密钥
配置ANDROID STUDIO
编写项目代码
运行项目
需求展示
利用百度地图API定位到当前所在位置,并显示所在的经纬度和地址信息。
效果展示:
申请百度地图API密钥
首先参考百度地图官方文档申请和注册密钥AK:androidsdk | 百度地图API SDK
在此获得包名:
输入申请到的SHA1码:
如何获得SHA1码呢?
①首先找到SDK安装目录,找到.android,继续找到debug.keystore,生成绝对路径,我这里是D:\androidsdk\.android\debug.keystore,复制待用。
②然后进入ANDROID STUDIO的终端Terminal,输入以下代码:
keytool -list -v -keystore 你自己的路径 -alias androiddebugkey
输入默认密钥库口令:
android
复制以下的SHA1码,黏贴到发布版SHA1码,生成百度控制台应用:
这样,百度地图API的密钥就申请完毕了。
配置ANDROID STUDIO
复制AK码,黏贴到application标签内,activity标签外:
<meta-dataandroid:name="com.baidu.lbsapi.API_KEY"android:value="开发者 key" />
下载开发SDK:SDK下载 - 百度LBS开放平台
选择基础定位功能和基础地图SDK选项:
选择jar包:
解压后黏贴到lib目录下,并将BaiduLBS_Android.jar包添加到依赖:
以上,ANDROID STUDIO中的百度地图配置已经完成。
编写项目代码
编写MainActivity的代码:
public class MainActivity extends AppCompatActivity {LocationClient mLocationClient; //定位客户端MapView mapView; //Android Widget地图控件BaiduMap baiduMap;boolean isFirstLocate = true;TextView tv_Lat; //纬度TextView tv_Lon; //经度TextView tv_Add; //地址@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//如果没有定位权限,动态请求用户允许使用该权限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();}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {switch (requestCode) {case 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());SDKInitializer.initialize(getApplicationContext());setContentView(R.layout.activity_main);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 {//获取纬度信息@Overridepublic 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;}}@Overrideprotected void onResume() {super.onResume();mapView.onResume();}@Overrideprotected void onPause() {super.onPause();mapView.onResume();}@Overrideprotected void onDestroy() {super.onDestroy();mLocationClient.stop();mapView.onDestroy();}}
编写activity_main.xml:
<FrameLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><!--百度地图控件--><com.baidu.mapapi.map.MapViewandroid:id="@+id/bmapView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:clickable="true" /><!--位置文本布局的背景色代码的前2位代码为透明度--><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#e0000000"android:orientation="vertical" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="12dp"android:layout_marginTop="20dp"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="纬度:"android:textColor="#ffffff"android:textSize="15dp" /><TextViewandroid:id="@+id/tv_Lat"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:textColor="#ffffff"android:textSize="15dp" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="12dp"android:layout_marginTop="10dp"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="经度:"android:textColor="#ffffff"android:textSize="15dp" /><TextViewandroid:id="@+id/tv_Lon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:textColor="#ffffff"android:textSize="15dp" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:layout_marginLeft="12dp"android:layout_marginTop="10dp"android:orientation="horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="地址:"android:textColor="#ffffff"android:textSize="15dp" /><TextViewandroid:id="@+id/tv_Add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:textColor="#ffffff"android:textSize="15dp" /></LinearLayout></LinearLayout></FrameLayout>
在manifest.xml中添加申请权限:
package="edu.hubu.mydbs"><!-- 这个权限用于进行网络定位--><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><!-- 这个权限用于访问GPS定位--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位--><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/><!-- 访问网络,网络定位需要上网--><uses-permission android:name="android.permission.INTERNET"/><!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 --><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在manifest.xml文件中的application标签中添加service声明:
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/>
运行项目
由于ANDRID STUDIO中的虚拟机通过网络定位,故运行项目后定位一直显示在不正确的位置。
故我选择了直接在手机上进行USB调试,使用了小米手机。
具体如何连接手机调试USB,我参考了下面这篇文章:
Android studio 连接手机调试_星夜之北的博客-CSDN博客
附上完整的代码:GitHubContribute to BakerStreetL/MyDBSbaidu development by creating an account on GitHub./BakerStreetL/MyDBSbaidu.git