开发百度地图定位APP

开发百度地图定位APP

开发步骤:

  1. 切换界面:新建一个project,并切换至Project界面在这里插入图片描述

  2. 下载百度地图SDK,使得文件架构如图所示:在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

  3. 获取本机的Android指纹码——SHA:
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述7C:DD:4E:08:D4:99:31:01:C3:0A:39:5E:9F:44:48:21:FC:57:C9:18

  4. 进入百度地图开发者页面、注册和登录
    在这里插入图片描述
    在这里插入图片描述

  5. 在模块的清单文件中添加百度所需权限:(该权限需要添加到Manifest文件中,application代码段外)注意:该权限需要添加到Manifest文件中,application代码段外

  6. 在清单文件Manifest中,在Activity组件注册的代码后,添加注册远程服务和配置应用Key的代码:在这里插入图片描述如出现com.baidu.location.f报错的情况,很有可能是因为下载的百度jar包不正确。

  7. 修改activity_main.xml文件,可根据自身需求在地图的基础上进行添加:`<com.baidu.mapapi.map.MapView
    android:id="@+id/bmapView"
    android:layout_width=“match_parent”
    android:layout_height=“match_parent”
    android:clickable=“true” />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="纬度:"
            android:textColor="#ffffff"
            android:textSize="15dp" />
    
        <TextView
            android:id="@+id/weidu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="15dp" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="经度:"
            android:textColor="#ffffff"
            android:textSize="15dp" />
    
        <TextView
            android:id="@+id/jingdu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="15dp" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="地址:"
            android:textColor="#ffffff"
            android:textSize="15dp" />
    
        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="15dp" />
    </LinearLayout>
    

    `

  8. 修改MainActivuty Java文件:
    在onCreate函数中对TextView和MapView组件进行定义,同时引用定位初始化:

setContentView(R.layout.activity_main);

        mMapView = findViewById(R.id.bmapView);
        weidu=findViewById(R.id.weidu);
        jingdu=findViewById(R.id.jingdu);
        address=findViewById(R.id.address);
        baiduMap=mMapView.getMap();
        initLocation();

在initLocation()函数中进行定位的初始化,启动定位并保存定位信息:

baiduMap.setMyLocationEnabled(true);
        mLocationClient=new LocationClient(this);
        MyLocationListener myListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        // 打开gps
        option.setOpenGps(true);
        // 设置坐标类型
        option.setCoorType("bd09ll");
        option.setScanSpan(1000);
        mLocationClient.setLocOption(option);
        mLocationClient.start();

定义百度位置监听器MyLocationListener,将保存的定位信息传到TextView
组件中,并设置定位数据:

public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null || mMapView == null) {
                return;
            }
            weidu.setText(bdLocation.getLatitude()+"");
            jingdu.setText(bdLocation.getLongitude()+"");
            address.setText(bdLocation.getAddrStr());
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(bdLocation.getRadius())// 设置定位数据的精度信息,单位:米
                    .direction(bdLocation.getDirection()) // 此处设置开发者获取到的方向信息,顺时针0-360
                    .latitude(bdLocation.getLatitude())
                    .longitude(bdLocation.getLongitude())
                    .build();
            // 设置定位数据, 只有先允许定位图层后设置数据才会生效
            baiduMap.setMyLocationData(locData);
            if (isFirstLocate) {
                isFirstLocate = false;
                LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(latLng).zoom(20.0f);
                baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }

结果展示:
在这里插入图片描述
连接手机,将手机作为虚拟机后运行出现的界面:
在这里插入图片描述
遇到的问题及解决方案:
在这里插入图片描述
在出现以上问题并确认在控制台中所输入的信息无误时,可以核查一下求SHA1值的方法有没有出现错误,1图和2图中均可求出来SHA1值,但两个值并不相同,1图中的方法对于在Android中进行定位这一代码来说是有误的,2图的方法才正确。:
在这里插入图片描述在这里插入图片描述
问题2:
在添加注册远程服务和配置应用Key的代码时出现如下代码的报错,可以检查一下下载的JAR包是否出现错误或者选择JAR包时未选择正确:
在这里插入图片描述

代码仓库
开发百度地图定位APP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值