融合定位开发

news/2025/3/21 12:17:50/

融合定位开发

配置权限

<!--    融合定位开发权限--><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

添加依赖

implementation 'com.huawei.hms:location:5.1.0.303'

动态申请定位权限

 //动态申请权限private void setIntegration() {if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {Log.i(TAG, "android sdk <= 28 Q");if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {String[] strings ={Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};ActivityCompat.requestPermissions(this, strings, 1);}} else {// Android SDK>28 所需权限动态申请,需添加“android.permission.ACCESS_BACKGROUND_LOCATION”权限if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this,"android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {String[] strings = {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,"android.permission.ACCESS_BACKGROUND_LOCATION"};ActivityCompat.requestPermissions(this, strings, 2);}}}

检查设备定位开关状态

private void locationFunction() {SettingsClient settingsClient = LocationServices.getSettingsClient(this);LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();LocationRequest mLocationRequest = new LocationRequest();builder.addLocationRequest(mLocationRequest);LocationSettingsRequest locationSettingsRequest = builder.build();// 检查设备定位设置settingsClient.checkLocationSettings(locationSettingsRequest)// 检查设备定位设置接口调用成功监听.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {@Overridepublic void onSuccess(LocationSettingsResponse locationSettingsResponse) {Log.i(TAG, "onSuccess: 成功!");LocationSettingsStates locationSettingsStates =locationSettingsResponse.getLocationSettingsStates();StringBuilder stringBuilder = new StringBuilder();// 定位开关是否打开stringBuilder.append(",\nisLocationUsable=").append(locationSettingsStates.isLocationUsable());// HMS Core是否可用stringBuilder.append(",\nisHMSLocationUsable=").append(locationSettingsStates.isHMSLocationUsable());Log.i(TAG, "checkLocationSetting onComplete:" + stringBuilder.toString());Log.i(TAG, "onSuccess: locationSettingsResponse=====" + locationSettingsResponse.toString());}})// 检查设备定位设置接口失败监听回调.addOnFailureListener(new OnFailureListener() {@Overridepublic void onFailure(Exception e) {Log.i(TAG, "checkLocationSetting onFailure:失败!!!!!!!" + e.getMessage());}});}

获取当前定位服务

 //获取当前定位服务private void positioningFunction() {FusedLocationProviderClient fusedLocationProviderClient = new FusedLocationProviderClient(this);LocationRequest mLocationRequest = new LocationRequest();设置定位类型mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);//设置定位类型  高进度
// 设置回调次数为1
//        mLocationRequest.setNumUpdates(1);mLocationRequest.setInterval(10000);//持续定位LocationCallback locationCallback = new LocationCallback() {@Overridepublic void onLocationResult(LocationResult locationResult) {Location lastLocation = locationResult.getLastLocation();//打印当前设备的经纬度Log.i(TAG, "onLocationResult: 定位位置" + lastLocation.getLongitude() +"=="+ lastLocation.getLatitude());}};//介绍了两种方法   以教辅资料为准  开发文档为辅助
//        Task<Location> lastLocation = fusedLocationProviderClient.getLastLocation();
//        lastLocation.addOnSuccessListener(new )fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, locationCallback, Looper.getMainLooper());}

页面

<Buttonandroid:id="@+id/bt_1"android:text="检测设备状态"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/bt_2"android:text="定位服务"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:orientation="vertical"android:gravity="center"

后台代码

  private Button bt1;private Button bt2;绑定按钮bt1 = findViewById(R.id.bt_1);bt1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {locationFunction();}});bt2 = findViewById(R.id.bt_2);bt2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {positioningFunction();}});


http://www.ppmy.cn/news/486610.html

相关文章

在芯片层面做定位技术融合靠谱吗

在芯片层面做定位技术融合靠谱吗 现如今的定位系统呈现了多种技术融合的大趋势&#xff0c;基本上单一技术的定位技术所出的定位产品越来越少&#xff0c;常规做法是采用不同定位技术的专有芯片进行电路板级别的融合&#xff0c;然后外围电源、天线、MCU等公用的方式进行技术融…

高精度定位系统融合定位模式崭露头角

进入物联网时代后&#xff0c;数据的重要价值进一步凸显&#xff0c;位置信息也可谓是重中之重。将高精度定位系统与物联网等等各类信息技术的融合让数字化应用得以建立在更精确的时空信息之上&#xff0c;促使产业端的智能升级带来了大量的创新应用。四相科技在应用市场需求的…

【多传感器融合定位】

多传感器融合定位 本文记录多传感器融合定位系列的环境配置相关问题 原文&#xff1a; 多传感器融合定位 第一章 概述. 环境安装 ubuntu18.04 ROS Melodic 1.g2o // 从github上下载源码 $ https://github.com/RainerKuemmerle/g2o/tree/20170730_git// 安装依赖 $ sudo a…

融合定位的优势

大概不少人都有这样的经历&#xff1a;在大型商场或者火车站里找不到自己的位置&#xff0c;甚至不知道应该走哪个方向。而在这个时代里&#xff0c;很多人会问自己“能不能用手机或者一些科技神器帮忙找路呢&#xff1f;”当然可以&#xff0c;其中有一个新兴技术&#xff0c;…

IMUGPS融合定位::IMU姿态解算

姿态解算 1. 背景 姿态解算是飞控的一个基础、重要部分&#xff0c;估计出来的姿态会发布给姿态控制器&#xff0c;控制飞行平稳&#xff0c;是飞行稳定的最重要保障。另外&#xff0c;姿态解算不仅仅用于无人机领域&#xff0c;无人车领域也需要进行姿态解算&#xff0c;用以…

多传感器融合定位 第一章 概述

多传感器融合定位 第一章 概述 本记录深蓝学院多传感器定位融合第四期学习笔记&#xff0c;官方推荐使用docker进行开发&#xff0c;为了方便之后移植部署&#xff0c;故本次在次在本地环境进行开发。 代码下载 &#xff1a; https://github.com/kahowang/sensor-fusion-for-…

多传感器融合定位十五-多传感器时空标定(综述)

多传感器融合定位十五-多传感器时空标定 1. 多传感器标定简介1.1 标定内容及方法1.2 讲解思路 2. 内参标定2.1 雷达内参标定2.2 IMU内参标定2.3 编码器内参标定2.4 相机内参标定 3. 外参标定3.1 雷达和相机外参标定3.2 多雷达外参标定3.3 手眼标定3.4 融合中标定3.5 总结 4. 时…

多传感器融合定位(二)——基于地图的定位

目录 一、回环检测 1.1 基于Scan Context 1.2 基于直方图 一、回环检测 回环检测只能消除一部分误差&#xff0c;不能消除全部误差。运用视觉用特征点描述子比较简单。 1.1 基于Scan Context 三维降二维&#xff0c;用图像做匹配。 1、划分网格 2、生成scan contest 3、…