Android studio 百度地图开发之2022(2)地图显示与定位

附上百度官网开发指南之显示地图:

androidsdk | 百度地图API SDK (baidu.com)https://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-map/showmap这里简述一下我所遇到的问题:(开发指南上有的我就不提了,反正就是照着一步一步肝)

1.Caused by: java.lang.reflect.InvocationTargetException

  Caused by: java.lang.NullPointerException

解决:

SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);

这两个位置先后写反了,要先初始化再加载布局文件

2.真机调试,而不是用avd

否则定位在美国的谷歌

3.vivo真机调试

下载SDK stools的Google USB Driver

在设置里打开设备管理器中,(windows10)点击查看,显示隐藏的设备,找到便携设备安装驱动到对应手机上(下载的Google USB Driver地址)

(3.1此时手机已经打开开发者选项,选择默认USB配置为:MIDI)

(3.2 在gradle.properties中设置

android.injected.testOnly=false

运行对应vivo,手机即可下载与定位

4.中间还遇到好多问题,反正就查嘛,忘了(坚持就是胜利,出了问题就解决问题)

结果如下

模拟器:

真机运行

真机定位几内亚湾原因:android6.0以上动态授予权限(我的是11)

解决办法:应用管理中找到应用,在权限中开启“定位”

 下面附上我的实验代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  <com.baidu.mapapi.map.MapView
      android:id="@+id/bmapView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="true" />
  <TextView
      android:id="@+id/text_tishi"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="原始的textView"
      android:layout_marginTop="20dp"
      android:textSize="13sp"
      android:gravity="center"
      android:padding="10dp"/>

 
  </RelativeLayout>
public class MainActivity extends AppCompatActivity {
    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;
    private LocationClient mLocationClient = null;

    private TextView mtextView;
    // 是否是第一次定位
    private boolean isFirstLocate = true;
    // 当前定位模式
    private MyLocationConfiguration.LocationMode locationMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);

        mMapView = findViewById(R.id.bmapView);
        mtextView = findViewById(R.id.text_tishi);

        //开启交通图
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setTrafficEnabled(true);
        //开启地图的定位图层
        mBaiduMap.setMyLocationEnabled(true);
//        BaiduMapOptions options = new BaiduMapOptions();
//        options.mapType(BaiduMap.MAP_TYPE_SATELLITE);
//        MapView mapView = new MapView(this, options);
//        setContentView(mapView);卫星地图view显示

        //定位初始化
        mLocationClient = new LocationClient(this);

//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
// 可选,设置地址信息
        option.setIsNeedAddress(true);
        //可选,设置是否需要地址描述
        option.setIsNeedLocationDescribe(true);


//设置locationClientOption
        mLocationClient.setLocOption(option);

//注册LocationListener监听器
        MyLocationListene myLocationListener = new MyLocationListene();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();
    }


    public class MyLocationListene extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }

            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            if (isFirstLocate) {
                isFirstLocate = false;
                //给地图设置状态
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(ll));
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);

            // 更换定位图标,这里的图片是放在 drawble 文件下的
            BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
            // 定位模式 地图SDK支持三种定位模式:NORMAL(普通态), FOLLOWING(跟随态), COMPASS(罗盘态)
            locationMode = MyLocationConfiguration.LocationMode.NORMAL;
            // 定位模式、是否开启方向、设置自定义定位图标、精度圈填充颜色以及精度圈边框颜色5个属性(此处只设置了前三个)。
            MyLocationConfiguration mLocationConfiguration = new MyLocationConfiguration(locationMode,true,mCurrentMarker);
// 使自定义的配置生效
            mBaiduMap.setMyLocationConfiguration(mLocationConfiguration);

            // 显示当前信息
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("\n经度:" + location.getLatitude());
            stringBuilder.append("\n纬度:"+ location.getLongitude());
            stringBuilder.append("\n状态码:"+ location.getLocType());
            stringBuilder.append("\n国家:" + location.getCountry());
            stringBuilder.append("\n城市:"+ location.getCity());
            stringBuilder.append("\n区:" + location.getDistrict());
            stringBuilder.append("\n街道:" + location.getStreet());
            stringBuilder.append("\n地址:" + location.getAddrStr());

            mtextView.setText(stringBuilder.toString());





        }
    }


    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mLocationClient.stop();
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }
}

<uses-permission android:name="android.permission.INTERNET" />
    <!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- 这个权限用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <!-- 这个权限用于访问GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

权限我多给了很多,懒得删了,代码中也有冗余

注:

1.

R.drawable.icon_gcoding

 图片icon_gcoding.png我随便找了个demo包里找的下载的

2.

private MapView mMapView = null;
private BaiduMap mBaiduMap = null;
private LocationClient mLocationClient = null;
private TextView mtextView;
private boolean isFirstLocate = true;
private MyLocationConfiguration.LocationMode locationMode;

 把私有变量提前定义好

参考大佬代码源地址:

Android 百度地图定位显示当前位置_虚言假面的博客-CSDN博客_安卓百度地图定位文章目录配置百度地图获取 SHA1获取百度地图密钥(AK)配置 Android Studio显示地图配置 AndroidManifest.xml 文件在布局文件中添加地图容器初始化地图管理MapView生命周期定位到当前位置配置 AndroidManifest.xml 文件布局编码状态码其实百度地图的官方文档已经很详细了,按照文档中的步骤来进行配置就能行。在此记录一下 Android 中引入百...https://blog.csdn.net/qq_41032474/article/details/105897263以及百度开发指南

respect!!!

Guess you like

Origin blog.csdn.net/chj9182020/article/details/122365221