Android初级开发(十)——百度定位—在地图上显示你的位置
程序员文章站
2022-04-19 21:27:41
...
在上一篇的基础上进行开发
一、布局文件
使用了LBS提供的地图View控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/position_text_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:clickable="true"/>
</LinearLayout>
二、MainActivity.java
public class MainActivity extends AppCompatActivity {
public LocationClient mLoacationClient;
private TextView positionText;
private MapView mapView;
private BaiduMap baiduMap;
private boolean isFirstLocation = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//创建一个LocationClient的实例,接受一个Context参数
mLoacationClient = new LocationClient(getApplicationContext());
//调用LocationClient的registerLocationListener()方法注册一个定位监听器
//当获取到位置信息时,就会回调这个定位监听器
mLoacationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
positionText = (TextView) findViewById(R.id.position_text_view);
mapView = (MapView) findViewById(R.id.bmapView);
baiduMap = mapView.getMap();
baiduMap.setMyLocationEnabled(true);
//创建一个空的List集合
//依次判断这3个权限有没有被授权,如果没有被授权就添加到List集合中
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);
}
//将List转换成数据
//调用ActivityCompat。requestPermissions()方法一次性申请
if(!permissionList.isEmpty()){
String[] permissions = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else{
requestLocation();
}
}
//开始地理位置定位
private void requestLocation() {
//更新地理位置
initLocation();
//调用LocationClient的start()方法开始定位
//定位的结果会回调到前面注册的监听器中
mLoacationClient.start();
}
private void initLocation() {
//创建一个LocationClientOption对象
LocationClientOption option = new LocationClientOption();
//设置更新的间隔,单位毫秒
option.setScanSpan(5000);
//表示我们需要获取当前位置的详细地址信息
option.setIsNeedAddress(true);
mLoacationClient.setLocOption(option);
}
@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:
}
}
private void navigateTo(BDLocation location){
//第一次定位时让地图定位到我们当前位置
//之后地图上显示的位置随设备的移动而实时改变
if(isFirstLocation){
//LatLng类主要是用于存放经纬度的值
//第一个参数是纬度值,第二个参数是经度值
LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
//设置地图的缩放级别,zoomTo()方法接受一个float型的参数
update = MapStatusUpdateFactory.zoomTo(16f);
baiduMap.animateMapStatus(update);
isFirstLocation = false;
}
//添加MyLocationData的构建逻辑,将Location中包含的经度和纬度封装到MyLocationData.Builder中
MyLocationData.Builder locationBuilder = new MyLocationData.Builder();
locationBuilder.latitude(location.getLatitude());
locationBuilder.longitude(location.getLatitude());
MyLocationData locationData = locationBuilder.build();
baiduMap.setMyLocationData(locationData);
}
public class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {
StringBuilder currentPosition = new StringBuilder();
currentPosition.append("纬度:").append(location.getLatitude());
currentPosition.append("经线:").append(location.getLongitude());
currentPosition.append("省:").append(location.getProvince());
currentPosition.append("市:").append(location.getCity());
currentPosition.append("区:").append(location.getDistrict());
currentPosition.append("街道:").append(location.getStreet());
currentPosition.append("定位方式:");
if(location.getLocType() == BDLocation.TypeGpsLocation){
currentPosition.append("GPS");
}else if(location.getLocType() == BDLocation.TypeNetWorkLocation){
currentPosition.append("网络");
}
positionText.setText(currentPosition);
navigateTo(location);
}
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
//当活动被销毁的时候调用LocationClient的stop()方法来停止定位
mLoacationClient.stop();
mapView.onDestroy();
baiduMap.setMyLocationEnabled(false);
}
}
三、效果图
没有出现小光标显示出我的位置,问题出在哪里还要继续看一下。。。
上一篇: react中调用百度地图
下一篇: 作业2