Android学习笔记—— 十五 、基于位置的服务
程序员文章站
2022-05-11 14:49:31
...
因为谷歌的定位API无法使用,所以使用百度地图的API:百度地图开放平台
1. 注册和获取**
- 官方教程:注册和获取**
2. 下载百度地图开放平台的SDK
-
下载位置:Android 地图 SDK
-
下载及配置SDK参考官方教程:Android Studio工程配置
3. Android Studio的相关配置流程
- 官方教程:Android Studio配置
4. 基本使用方法
参考官方文档(因为我尝试了一下写总结,结果发现怎么写都不如看官方文档 ×_× ):!
PS:运行时权限处理同时申请多种权限的一种方法
//运行时权限处理,同时进行多个运行时权限处理的一种方法
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.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()) {
String[] permission = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this, permission, 1);
} else {
requestLocation();
}
练习的示例
public class MainActivity extends AppCompatActivity {
public LocationClient locationClient;
public LocationClientOption locationClientOption;
private TextView tv_showLocation;
private MapView mapView;
private BaiduMap baiduMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化MapView,而且必须书写在setContentView方法之前,否则会报错
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
tv_showLocation = findViewById(R.id.tv_showLocation);
mapView = findViewById(R.id.mapView);
//初始化LocationClient类
locationClient = new LocationClient(getApplicationContext());
locationClient.registerLocationListener(new MyLocationListener());
//配置定位选项
locationClientOption = new LocationClientOption();
//设置两次定位间隔时间,单位为毫秒
locationClientOption.setScanSpan(5000);
//允许使用GPS
locationClientOption.setOpenGps(true);
//设置需要获取地址信息
locationClientOption.setIsNeedAddress(true);
//允许模拟的GPS,否则模拟器无法显示经纬度
locationClientOption.setEnableSimulateGps(true);
//加载设置
locationClient.setLocOption(locationClientOption);
//设置缩放程度
baiduMap = mapView.getMap();
MapStatusUpdate update = MapStatusUpdateFactory.zoomBy(3.5f);
baiduMap.animateMapStatus(update);
//设置mapView显示当前所在位置的蓝点
baiduMap.setMyLocationEnabled(true);
//运行时权限处理
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.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()) {
String[] permission = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this, permission, 1);
} else {
requestLocation();
}
}
//自定义方法,启动定位
private void requestLocation() {
locationClient.start();
}
//运行时权限结果处理
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 1) {
if (grantResults.length > 0) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "必须同意所有权限才可以使用本程序", Toast.LENGTH_SHORT).show();
finish();
return;
}
locationClient.restart();
}
requestLocation();
} else {
Toast.makeText(this, "Unknown Error! ", Toast.LENGTH_SHORT).show();
finish();
}
}
}
public class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
//将地图设置为以当前位置为中心
LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
//更新蓝点的位置
MyLocationData locationData = new MyLocationData.Builder()
.accuracy(bdLocation.getRadius())
.direction(bdLocation.getDirection())
.latitude(bdLocation.getLatitude())
.longitude(bdLocation.getLongitude())
.build();
baiduMap.setMyLocationData(locationData);
//获取经纬度和地址信息
StringBuilder currentPosition = new StringBuilder();
currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n")
.append("经度:").append(bdLocation.getLongitude()).append("\n")
.append("国家:").append(bdLocation.getCountry()).append("\n")
.append("省:").append(bdLocation.getProvince()).append("\n")
.append("城市:").append(bdLocation.getCity()).append("\n")
.append("区:").append(bdLocation.getDistrict()).append("\n")
.append("街道:").append(bdLocation.getStreet()).append("\n")
.append("定位方式:");
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
currentPosition.append("GPS");
} else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
currentPosition.append("Network");
} else {
//如果不是以上定位方式,显示定位方式代码并和官方文档对照检查问题
currentPosition.append(bdLocation.getLocType()).append(" 定位类型未知或定位出错!");
}
tv_showLocation.setText(currentPosition);
}
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
//关闭蓝点显示,停止定位,销毁mapView
baiduMap.setMyLocationEnabled(false);
locationClient.stop();
mapView.onDestroy();
}
}
上一篇: 嵌入式Linux-JZ2440-04课-存储管理器代码的分析
下一篇: linux文件管理