android app控制ros机器人五(百度地图)

news/2024/7/23 10:24:35 标签: android, ros-mobile, android app, 百度地图

半吊子改安卓,新增了标签页,此标签页需要显示百度地图

按照官方教程注册信息,得到访问应用AK,步骤也可以参照下面csdn

Android地图SDK | 百度地图API SDK

【Android】实现百度地图显示_宾有为的博客-CSDN博客

本人使用的是aar开发包,ros-mobile工程中app下没有libs文件夹需要新建。把开发包libs下的文件复制到工程中的libs。在app下的build.gradle中添加了如下代码。

implementation files('libs/BaiduLBS_Android.aar') // 添加这一行,替换为你的 AAR 文件名

查阅资料了解到,百度地图SDK初始化在程序入口进行较好,可以避免多次初始化或冲突问题。

MainActivity.java中添加:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main); //置当前活动使用的布局文件为 activity_main.xml

        // 同意百度地图的隐私政策
        SDKInitializer.setAgreePrivacy(getApplicationContext(), true);
        // 初始化百度地图 SDK
        SDKInitializer.initialize(getApplicationContext());
        SDKInitializer.setCoordType(CoordType.BD09LL);

        try {
......//其他代码

对应.xml文件:

 <!-- 百度地图组件 -->
        <com.baidu.mapapi.map.MapView
            android:id="@+id/baiduMapView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:visibility="visible" />

 对应fragment.java文件:

package com.schneewittchen.rosandroid.ui.fragments.map;

//.....import其他包

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;




public class MapFragment extends Fragment {

    private MapView mapView;
    private BaiduMap baiduMap;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, container, false);

        mapView = rootView.findViewById(R.id.baiduMapView); // 获取组件
        Log.d("MapFragment", "MapView is null: " + (mapView == null));

        baiduMap = mapView.getMap();
        MapStatusUpdate update = MapStatusUpdateFactory.zoomTo(15);
        baiduMap.setMapStatus(update);

        return rootView;
    }
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mapView.onDestroy();

    }
}

最终效果:

 增加定位功能:

第一版本,可以显示定位蓝点,但是定位有误差,偏差几个街道,此方法不稳定,第二次进入该标签页测试时会出现定位点无法显示的情况

package com.schneewittchen.rosandroid.ui.fragments.map;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.schneewittchen.rosandroid.R;
import java.util.Map;

public class MapFragment extends Fragment {

    private MapView mapView;
    private BaiduMap baiduMap;
    private LocationManager locationManager;
    private static final int LOCATION_PERMISSION_REQUEST = 101;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, container, false);

        mapView = rootView.findViewById(R.id.baiduMapView); // 获取组件
        baiduMap = mapView.getMap();
        // 启用定位图层
        baiduMap.setMyLocationEnabled(true);

        MapStatusUpdate update = MapStatusUpdateFactory.zoomTo(18);
        baiduMap.setMapStatus(update);

        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        locationManager = (LocationManager) requireContext().getSystemService(Context.LOCATION_SERVICE);

        if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Log.d("MapFragment", "Location permission granted.");
            startLocationUpdates();
        } else {
            ActivityCompat.requestPermissions(requireActivity(), new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
            }, LOCATION_PERMISSION_REQUEST);
        }
    }

    private void startLocationUpdates() {
        LocationManager locationManager = (LocationManager) requireContext().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);

        if (provider != null) {
            Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
            if (lastKnownLocation != null) {
                Log.d("MapFragment", "Last known location: " + lastKnownLocation.getLatitude() + ", " + lastKnownLocation.getLongitude());

                // Move the camera to the current location
                baiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())));

                // Configure and show the blue dot
                MyLocationConfiguration configuration = new MyLocationConfiguration(
                        MyLocationConfiguration.LocationMode.NORMAL,
                        true,
                        null);
                baiduMap.setMyLocationConfiguration(configuration);
                Log.d("MapFragment", "Current location set on the map.");

                // Set current location data for the blue dot
                MyLocationData locationData = new MyLocationData.Builder()
                        .accuracy(lastKnownLocation.getAccuracy())
                        .latitude(lastKnownLocation.getLatitude())
                        .longitude(lastKnownLocation.getLongitude())
                        .build();
                baiduMap.setMyLocationData(locationData);
                Log.d("MapFragment", "Current location set on the map.");
            }else{
                Log.d("MapFragment", "Last known location is null.");
            }
        }else{
            Log.d("MapFragment", "Location provider is null.");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == LOCATION_PERMISSION_REQUEST) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("MapFragment", "Location permission granted.");
                startLocationUpdates();
            } else {
                Log.d("MapFragment", "Location permission denied.");
                // Handle permission denied
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mapView.onDestroy();
    }
}

按照百度地图提供的关于定位教程无法实现定位功能,初始化客户端的时候总是报错,按照提示使用try,会出现闪退无法显示地图,网上也有看到相同错误,但是没找到解决适用的方法。有解决该问题的欢迎交流!

// 初始化客户端

mLocationClient = new LocationClient(requireContext());

http://www.niftyadmin.cn/n/4938763.html

相关文章

通过网关访问微服务,一次正常,一次不正常 (nacos配置的永久实例却未启动导致)

微服务直接访问没问题&#xff0c;通过网关访问&#xff0c;就一次正常访问&#xff0c;一次401错误&#xff0c;交替正常和出错 负载均衡试了 路由配置检查了 最后发现nacos下竟然有2个order服务实例&#xff0c;我明明只开启了一个呀 原来之前的8080端口微服务还残留&…

Gof23设计模式之模板方法模式

1.定义 定义一个操作中的算法骨架&#xff0c;而将算法的一些步骤延迟到子类中&#xff0c;使得子类可以不改变该算法结构的情况下重定义该算法的某些特定步骤。 2.结构 模板方法&#xff08;Template Method&#xff09;模式包含以下主要角色&#xff1a; 抽象类&#xff0…

项目中怎么做sql优化?

背景&#xff1a; 系统用着用着突然出现卡&#xff0c;数据加载慢。这个时候有可能是sql查询问题导致的。这个时候我们要怎么排查这个问题呢。如果排查后是sql问题的话我们应该怎么优化呢&#xff01; 处理方案&#xff1a;第一步定位 可以开启MySQL的慢查询日志&#xff0c;设…

一站式自动化测试平台-Autotestplat

3.1 自动化平台开发方案 3.1.1 功能需求 3.1.3 开发时间计划 如果是刚入门、但有一点代码基础的测试人员&#xff0c;大概 3 个月能做出演示版(Demo)进行自动化测试&#xff0c;6 个月内胜任开展工作中项目的自动化测试。 如果是有自动化测试基础的测试人员&#xff0c;大概 …

探讨uniapp的数据缓存问题

异步就是不管保没保存成功&#xff0c;程序都会继续往下执行。同步是等保存成功了&#xff0c;才会执行下面的代码。使用异步&#xff0c;性能会更好&#xff1b;而使用同步&#xff0c;数据会更安全。 1 uni.setStorage(OBJECT) 将数据存储在本地缓存中指定的 key 中&#x…

Redis辅助功能

一、Redis队列 1.1、订阅 subscribe ch1 ch2 1.2 publish:发布消息 publish channel message 1.3 unsubscribe: 退订 channel 1.4 模式匹配 psubscribe ch* 模糊发布&#xff0c;订阅&#xff0c;退订&#xff0c; p* <channelName> 1.5 发布订阅原理 订阅某个频道或…

牛客周赛 Round 7

目录 A 游游的you矩阵 题目&#xff1a; 题解&#xff1a; AC 代码&#xff1a; B 游游的01串操作 题目&#xff1a; 题解&#xff1a; AC 代码&#xff1a; C 游游的正整数 题目&#xff1a; 题解&#xff1a; AC 代码&#xff1a; D 游游的选数乘积 题目&#xf…

nginx一般轮询、加权轮询、ip_hash等负载均衡模式配置介绍

一.负载均衡含义简介 二.nginx负载均衡配置方式 准备三台设备&#xff1a; 2.190均衡服务器&#xff0c;2.191web服务器1&#xff0c;2.160web服务器2&#xff0c;三台设备均安装nginx&#xff0c;两台web服务器均有网页内容 1.一般轮询负载均衡 &#xff08;1&#xff09…