Android定位与百度定位
AS开发作业四:LBS
前言
这是AS在开发百度地图定位APP,显示出当前位置坐标的一个实验记录博客,如果博客中有内容描述错误或者代码有误,欢迎批评和指正。
提示:以下是本篇文章正文内容。
一、实验内容与界面展示
1. 实验内容与技术
1、根据官方文档,开发百度地图定位APP,显示出当前位置坐标。参考网址
2. 界面展示
二、实现过程
Android提供了三种定位方式(GPS定位、WiFI定位和基站定位)获得定位信息—经纬度【基站定位也称移动网络定位,也称为GPRS定位】
打开位置服务时,必须从三种定位模式(高精度—混合定位、节电—网络定位和单独的GPS定位)中选择一种,通常选择混合定位
GPS定位精度高,但可能由于GPS信号弱而较慢甚至获取不到
GPS定位对应于权限android.permission.ACCESS_FINE_LOCATION
网络定位(WiFi或移动网络)定位速度快,但精度相对GPS低
网络定位对应于权限android.permission.ACCESS_COARSE_LOCATION
1. 项目配置
- 注册和获取密钥
获取AK的流程大致可分为如下四个步骤:
(1)登录API控制台;(2)创建应用;(3)配置SHA1和包名;(4)提交生成AK。
(1)登录API控制台
API控制台的地址为:http://lbsyun.baidu.com/apiconsole/key
输入网址,进入API控制台。如果您还未登录,会显示如下页面,输入帐号及密码,点击登录,即可正常进入API控制台;
(2)创建应用
进入API控制台后,您将看到如下界面,点击创建应用,开始填写相关信息,并最终获得AK。
(3)配置SHA1和包名
a. 应用名称:开发者请自行定义,建议与应用名称一致,便于管理;
b. 应用类型:开发者请注意选择为Android SDK,选择其他类型,将导致所生成的AK不可用;(启用服务默认全部启用即可);
c. 填写SHA1和包名:这两个信息请开发者注意准确填写,它们是AK验证合法性的唯一依据。SHA1和包名的获取方式,请参考如下介绍。
(一)SHA1的获取方法:通过Android Studio获取
参考链接:Android Studio生成签名文件,自动签名,以及获取SHA1
(二)包名的获取方法:在Android Studio中获取
(4)提交生成AK
以上各项信息确认填写无误后,点击提交,系统自动生成AK。
- Android Studio配置
(1)打开/创建一个Android工程
(2)添加SDK(jar + so)
下载Android定位SDK并解压,将libs中的jar和so放置到工程中相应的位置。
注意,Android定位SDK提供了多种CPU架构的so文件(jar通用,只有一个),开发者可根据实际使用需求,放置所需so到对应的工程文件夹内。
下图为Android定位SDK放置到Android工程中的示意图:
配置build.gradle文件,注意设置sourceSets。
(3) 添加AK
开发者在使用SDK前,需完成AK申请,并在AndroidManifest.xml文件中,正确填写AK。
在Application标签中增加如下代码:
(4) 添加定位权限
使用定位SDK需添加如下权限:
2. activity_main.xml文件的相关配置添加
界面布局使用帧布局,包含有重叠效果的地图和位置文本。
activity_main.xml的代码如下:
<FrameLayout 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" />
<!--位置文本布局的背景色代码的前2位代码为透明度-->
<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_marginLeft="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="15dp" />
<TextView
android:id="@+id/tv_Lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="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="15dp" />
<TextView
android:id="@+id/tv_Lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="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="15dp" />
<TextView
android: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>
activity_main.xml页面设计的图片:
3. 创建DemoApplication类地图初始化
package com.example.lbs;
import android.app.Application;
import android.app.Application;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
public class MyLBS extends Application {
@Override
public void onCreate() {
super.onCreate();
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
SDKInitializer.initialize(this);
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
}
}
4. 界面程序MainActivity.java
定义LocationClient,位置监听,定义启动方式,定义定位模式,调取地图控件
百度地图应用,包含定位信息和地图显示
一般需要打开定位服务,选择高精度定位模式,有网络连接
并需要在AndroidManifest.xml文件里使用百度云服务
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
MainActivity.java架构:
MainActivity.java代码如下:
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; //地址
@Override
protected 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();
}
}
@Override
public 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(); //调用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 {
@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();
}
}
三、源码仓库
码云仓库地址:https://github.com/Julia-rs-zhu/LBS
总结
这一次实验博客记录主要是围绕百度地图的显示和定位来展开,首先我们直接进入到官网去配置,获取秘钥并下载了jar包,和之前写Java项目调用接口有点像,但是在加入包以后在AndroidManifest.xml文件里使用百度云服务时其com.baidu.lication.f一直报红,弄得我焦头烂额,在网上找解决方法也没找到,后来又重新弄了一次后来发现原来是按照老版本来的,所以出了不少的错误并且没有把jar包和其他.os文件放一起导致导报错误因此使得我的一直报红。
整个的实验过程中根据文档学习所以逻辑是比较顺畅:写入权限;地图初始化;定义LocationClient,位置监听,定义启动方式,定义定位模式,调取地图控件。
总体的呈现效果还是不错的,非常有成就感,总之这一次实验能够从底层了解安卓的地图调用和定位机制非常有收获,能自己动手做出来也比较开心。
今天就到这里啦,蟹蟹看到这里的盆友!共勉!