Android入门(17)| 百度提供的 Android定位SDK


配置百度提供的 Android定位SDK

详情参见官方文档,这里仅对获取 SHA1 做详细介绍:

用于发布的 SHA1

在这里插入图片描述

用于测试的 SHA1

在这里插入图片描述


使用百度定位实例

在这里插入图片描述

public class LocationActivity extends AppCompatActivity {
    
    
    private static final String TAG = "LocationActivity";

    public LocationClient locationClient;
    private TextView positionText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_layout);

        // setAgreePrivacy接口需要在LocationClient实例化之前调用
        // 如果setAgreePrivacy接口参数设置为了false,则定位功能不会实现
        // true,表示用户同意隐私合规政策
        // false,表示用户不同意隐私合规政策
        LocationClient.setAgreePrivacy(true);
        try {
    
    
        	// 通过getApplicationContext获取的全进程有效的Context来初始化LocationClient对象
            locationClient = new LocationClient(getApplicationContext());
        } catch (Exception e) {
    
    
            e.printStackTrace();
            Log.e(TAG, "onCreate: LocationClient初始化ERROR");
        }

        // 不同意隐私政策可能导致locationClient为空
        if (locationClient != null){
    
    
            // 注册监听器
            locationClient.registerLocationListener(new MyLocationClickListener());
        }

        positionText = findViewById(R.id.position_TextView);

        // 一次进行多个权限申请
        List<String> permissionList = new ArrayList<>();
        if (ContextCompat.checkSelfPermission(LocationActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
    
    
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (ContextCompat.checkSelfPermission(LocationActivity.this,
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
    
    
            permissionList.add(Manifest.permission.READ_PHONE_STATE);
        }
        if (ContextCompat.checkSelfPermission(LocationActivity.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(LocationActivity.this, permissions, 1);
        }
        else {
    
    
            requestLocation();
        }
    }

    public class MyLocationClickListener extends BDAbstractLocationListener {
    
    
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
    
    
            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("网络");
            }
            else {
    
    
                currentPosition.append("其他方式");
            }
            positionText.setText(currentPosition);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    
    
        switch (requestCode) {
    
    
            case 1:
                if (grantResults.length > 0){
    
    
                    for (int result : grantResults) {
    
    
                        if (result != PackageManager.PERMISSION_GRANTED) {
    
    
                            Toast.makeText(this, "必须同意所有权限才能使用本程序",
                                    Toast.LENGTH_LONG).show();
                            finish();
                            return;
                        }
                    }
                    requestLocation();
                }
                else {
    
    
                    Toast.makeText(this, "申请权限时发生未知错误", Toast.LENGTH_LONG)
                            .show();
                    Log.e(TAG, "onRequestPermissionsResult:待申请权限个数<=0");
                    finish();
                }
        }
    }

    private void requestLocation() {
    
    
        initLocation(); // 实时更新位置,没有该方法则只会定位一次
        locationClient.start(); // 开始定位
    }

	// 配置定位SDK参数
    private void initLocation() {
    
    
        LocationClientOption option = new LocationClientOption();
        option.setScanSpan(5000); // 5秒更新一次位置
        // 设置定位模式
        option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
        // 提供根据经纬度分析处于哪个国家、省、市、区、街道的功能
        option.setIsNeedAddress(true);
        locationClient.setLocOption(option);
    }

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        // 活动被销毁时通过stop()方法停止定位
        // 防止程序在后台持续定位消耗手机电量
        locationClient.stop();
    }
}

利用 LocationClientOption 类配置定位 SDK 参数:

LocationClientOption.setLocationMode() 可以设置定位模式,而 LocationClientOption.LocationMode 中有四种模式可选:

  • Hight_Accuracy: 高精度模式(默认模式),优先使用 GPS 定位,无法接受 GPS 信号时使用网络定位;
  • Fuzzy_Locating: 模糊定位模式,v9.2.8 版本开始支持,可以降低 API 的调用频率,但同时也会降低定位精度;
  • Battery_Saving: 节电模式,只会使用网络定位。
  • Device_Sensors: 传感器模式,只会使用 GPS 定位。

定位 SDK 能够返回三种坐标类型的经纬度(国内),分别是:

  • GCJ02(国测局坐标)
  • BD09(百度墨卡托坐标)
  • BD09ll(百度经纬度坐标),若想将定位 SDK 获得的经纬度直接在百度地图上标注,请选择该类型。

V6.2.3 版本起,全新升级了全球定位能力,在海外地区定位所获得的经纬度,坐标类型默认、且只能是WGS84类型。

猜你喜欢

转载自blog.csdn.net/Jormungand_V/article/details/123505439