Android基于位置的服务
1.基于位置的服务简介
基于位置的服务简称LBS,主要的工作原理是利用无线电通讯网络或GSP等定位方式来确定出移动设备所在的位置. GPS定位的工作原理是基于手机内置的GPS硬件直接和卫星交互来获取当前的经纬度信息,精确度高,只能在户外使用. 网络定位的工作原理是根据手机当前网络附近的三个基站进行测速,以此计算出手机和每个基站之间的距离,再通过三角形定位确定出一个大概的位置,精度一般,室内可用.
2.申请API Key
https://lbsyun.baidu.com/index.php?title=android-locsdk/geosdk-android-download
3.使用百度定位
1.准备LBS SDK
将下载的开发包中BaiduLBS_Android.jar放入app/libs/下,将其他文件夹放入src/main/jniLibs的新建目录 在app/build.gradle文件进行配置
dependencies {
implementation fileTree(dir: 'libs',includes: ['*.jar'])
...
}
2.确定自己位置的经纬度
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/position_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.lbstest">
<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.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LBSTest">
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="OIvUlM5kbVUlG7L3i3AERqDFCYlc1A7v"/>
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient;
private TextView positionText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient=new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
setContentView(R.layout.activity_main);
positionText=(TextView) findViewById(R.id.position_text_view);
List<String> permissionList=new ArrayList<>();
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!permissionList.isEmpty()){
String [] permissions=permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else{
requestLocation();
}
}
private void requestLocation(){
initLocation();
mLocationClient.start();
}
private void initLocation(){
LocationClientOption option=new LocationClientOption();
option.setScanSpan(5000);
mLocationClient.setLocOption(option);
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if(grantResults.length>0){
for (int result:grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "必须同意所有的权限才能使用本程序", Toast.LENGTH_SHORT).show();
finish();
return;
}
requestLocation();
}
}else{
Toast.makeText(this, "发生未知错误", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation bdLocation) {
runOnUiThread(new Runnable() {
@Override
public void run() {
StringBuilder currentPosition=new StringBuilder();
currentPosition.append("维度:").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("经线:").append(bdLocation.getLongitude()).append("\n");
currentPosition.append("定位方式:");
if(bdLocation.getLocType()==BDLocation.TypeGpsLocation){
currentPosition.append("GPS");
}else if(bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
currentPosition.append("网络");
}
positionText.setText(currentPosition);
}
});
}
}
}
3.选择定位模式
public class MainActivity extends AppCompatActivity {
private void initLocation(){
LocationClientOption option=new LocationClientOption();
option.setScanSpan(5000);
option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
mLocationClient.setLocOption(option);
}
...
}
百度LBS SDK的定位模式指定,有三种模式:Hight_Accuracy表示高精度模式,会在GPS信号正常的情况下优先使用GPS定位.Device_Sensors表示传感器模式,只会使用GPS定位.Battery_Saving表示节电模式,只会使用网络进行定位.(其中Height_Accuracy是默认的模式.)
4.其他位置信息
private void initLocation(){
LocationClientOption option=new LocationClientOption();
option.setScanSpan(5000);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation bdLocation) {
runOnUiThread(new Runnable() {
@Override
public void run() {
StringBuilder currentPosition=new StringBuilder();
currentPosition.append("维度:").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("经线:").append(bdLocation.getLongitude()).append("\n");
currentPosition.append("国家:").append(bdLocation.getCountry()).append("\n");
currentPosition.append("省:").append(bdLocation.getProvince()).append("\n");
currentPosition.append("市:").append(bdLocation.getCity()).append("\n");
currentPosition.append("区:").append(bdLocation.getDistrict()).append("\n");
currentPosition.append("街道:").append(bdLocation.getStreet()).append("\n");
currentPosition.append("定位方式:");
if(bdLocation.getLocType()==BDLocation.TypeGpsLocation){
currentPosition.append("GPS");
}else if(bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
currentPosition.append("网络");
}
positionText.setText(currentPosition);
}
});
}
4.使用百度地图
1.让地图显示出来
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/position_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
...
private MapView mapView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient=new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView=(MapView) findViewById(R.id.bmapView);
...
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
}
2.移动到我的位置
百度LBS SDK的API中提供了一个BaiduMap类,它是地图的总控制器,调用MapView的getMap()方法就能获取到BaiduMap的实例. BaiduMap baiduMap=mapView.getMap(); 有了BaiduMap后,就能够都地图进行操作. 百度地图将缩放级别的取值范围限定在3到19之间,其中小数点位的值也是可以取值的,值越大,地图显示的信息就越精细. MapStatusUpdate update=MapStatusUpdateFactory.zoomTo(12.5f); baiduMap.animateMapStatus(update); 其中MapStatusUpdateFactory的zoomTo()方法接收一个float型的参数,就是用于设置缩放级别的.zoomTo()方法返回一个MapStatusUpdate对象,我们把这个对象传入BaiduMap的animateMapStatus()方法当中即可完成缩放功能. 让地图移动到某一个经纬度.需要借助LatLng类.参数一:纬度值,参数二:经度值.之后调用MapStatusUpdateFactory的newLatLng()方法将LatLng对象传入,newLatLng()方法返回的也是一个MapStatusUpdate对象,再把这个对象传入BaiduMap的animateMapStatus()方法当中,就可以将地图移动到指定的经纬度上. LatLng ll=new LatLng(39.915,116.404); MapStatusUpdate update=MapStatusUpdateFactory.newLng(ll); baiduMap.animateMapStatus(update);
public class MainActivity extends AppCompatActivity {
...
private BaiduMap baiduMap;
private boolean isFirstLocate=true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient=new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView=(MapView) findViewById(R.id.bmapView);
baiduMap=mapView.getMap();
...
}
private void navigateTo(BDLocation bdlocation){
if (isFirstLocate){
LatLng ll=new LatLng(bdlocation.getLatitude(),bdlocation.getLongitude());
MapStatusUpdate update= MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
update=MapStatusUpdateFactory.zoomTo(16f);
baiduMap.animateMapStatus(update);
isFirstLocate=false;
}
}
private void navigateTo(BDLocation bdlocation){
if (isFirstLocate){
LatLng ll=new LatLng(bdlocation.getLatitude(),bdlocation.getLongitude());
MapStatusUpdate update= MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
update=MapStatusUpdateFactory.zoomTo(16f);
baiduMap.animateMapStatus(update);
isFirstLocate=false;
}
}
}
3.让"我"显示在地图上
百度LBS SDK当中提供了一个MyLocationData.Builer类,这个类是用来封装设备当前所在位置的,只需要把经纬信息传入到这个类的相应方法中 MyLocationData.Builder locationBuilder=new MyLocationData.Builder(); locationBuilder.latitude(39.915); locationBuilder.longitude(116.404); MyLocationData.Builder类还提供了一个build()方法,当我们要把封装的信息都设置完成之后,只需调用它的build()方法,就会生成一个MyLocationData的实例,然后再将这个实例传入到BaiduMap的setMyLocationData()方法中,就可以让设备当前的位置显示在地图上了. MylocationData locationData=locationBuilder.build(); baiduMap.setMyLocationData(locationData);
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationClient=new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView=(MapView) findViewById(R.id.bmapView);
baiduMap=mapView.getMap();
baiduMap.setMyLocationEnabled(true);
...
}
private void navigateTo(BDLocation bdlocation){
if (isFirstLocate){
LatLng ll=new LatLng(bdlocation.getLatitude(),bdlocation.getLongitude());
MapStatusUpdate update= MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
update=MapStatusUpdateFactory.zoomTo(16f);
baiduMap.animateMapStatus(update);
isFirstLocate=false;
}
MyLocationData.Builder locationBuilder=new MyLocationData.Builder();
locationBuilder.latitude(bdlocation.getLatitude());
locationBuilder.longitude(bdlocation.getLongitude());
MyLocationData locationData=locationBuilder.build();
baiduMap.setMyLocationData(locationData);
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
mapView.onDestroy();
baiduMap.setMyLocationEnabled(false);
}
|