百度地图定位(移动开发第四次作业)

一、注册和获取密钥

1.注册

搜索百度地图开发者
若有百度账号直接登录,如下图:在这里插入图片描述
没有百度账号,点击右下角根据提示注册并登录

2.获取密钥

  • 百度地图开发者官网

在这里插入图片描述

  • 登录后将进入API控制台,如下图:
    在这里插入图片描述

  • 点击“创建应用”开始申请开发密钥,如下图:
    在这里插入图片描述

  • 填写应用名称,注意应用类型选择“Android SDK”、正确填写SHA1 和 程序包名(SHA1和包名的获取方法见下文)。如图:
    在这里插入图片描述

  • 获取包名
    在app目录下的build.gradle文件中找到applicationId,并确保其值与AndroidManifest.xml中定义的package相同。
    在这里插入图片描述

  • 获取SHA1
    进入Terminal工具,如下图所示:
    在这里插入图片描述
    找到.android文件夹下的debug.keystroe
    在这里插入图片描述
    在控制台输入命令:keytool -list -v -keystore debug.keystore
    在这里插入图片描述
    输入默认密码android,查看SHA1
    在这里插入图片描述
    填入即可
    在这里插入图片描述

二、导入百度地图开发包

1.下载开发包

开发文档->Android 地图SDK->产品下载->自定义下载
在这里插入图片描述

2.将Jar包添加至项目

  • 在app目录下的build.gradle文件中android块中配置sourceSets标签
 sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }
  • 选择模块视图为Project,解压文件夹至项目libs
    在这里插入图片描述
  • 右键BaiduLBS_Android.jar选择Add As Library,此时会发现在app目录下的build.gradle的dependencies块中生成了工程所依赖的jar文件的对应说明,如下所示:在这里插入图片描述

三、显示地图

1. 配置AndroidManifest.xml文件

  • 在application中加入如下代码配置开发密钥(AK):
<application>  
    <meta-data  
        android:name="com.baidu.lbsapi.API_KEY"  
        android:value="开发者 key" />  
</application>

这个密钥是在百度开发者页面里生成的,需要替换。

  • 在application外部添加权限声明,需要声明的权限如下:
<!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 -->
<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" /> 

2. 在布局文件中添加地图容器

MapView是View的一个子类,用于在Android View中放置地图。MapView的使用方法与Android提供的其他View一样。

<com.baidu.mapapi.map.MapView  
    android:id="@+id/bmapView"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:clickable="true" />

3. 地图初始化

新建一个自定义的Application,在其onCreate方法中完成SDK的初始化。

public class DemoApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //在使用SDK各组件之前初始化context信息,传入ApplicationContext   
        SDKInitializer.initialize(this);
        //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
    }
}

在AndroidManifest.xml文件中声明该Application

android:name=".DemoApplication"

4.创建地图Activity,管理MapView生命周期

public class MainActivity extends Activity {  
    private MapView mMapView = null;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);  
        //获取地图控件引用  
        mMapView = (MapView) findViewById(R.id.bmapView);  
    }  
    @Override  
    protected void onResume() {  
       super.onResume();  
       //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理  
       mMapView.onResume();  
    }  
    @Override  
    protected void onPause() {  
      super.onPause();  
      //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理  
      mMapView.onPause();  
    } 
    @Override  
    protected void onDestroy() {  
      super.onDestroy();  
      //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理  
      mMapView.onDestroy();  
    }  
}

完成以上步骤即可显示:

在这里插入图片描述

四、定位地图

1. 配置AndroidManifest.xml文件

  • 加入如下权限使用声明
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  • 在Application标签中声明定位的service组件
<service android:name="com.baidu.location.f"
    android:enabled="true"
    android:process=":remote"/>

2.布局文件

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--百度地图控件-->
    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#e0000000"
        android:orientation="vertical">

        <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/tv_Lat"
                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/tv_Lon"
                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/tv_Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#ffffff"
                android:textSize="15dp" />
        </LinearLayout>
    </LinearLayout>
</FrameLayout>

3.开启地图的定位图层

  mBaiduMap = mMapView.getMap();
  mBaiduMap.setMyLocationEnabled(true);

4. 构造地图数据

我们通过继承抽象类BDAbstractListener并重写其onReceieveLocation方法来获取定位数据。

public class MyLocationListener extends BDAbstractLocationListener {
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    private boolean isFirstLocate = true;
    private LocationClientOption option;

    TextView tv_Lat;  //纬度
    TextView tv_Lon;  //经度
    TextView tv_Add;  //地址

    //构造方法用于传递地图控件
    public MyLocationListener(MapView mMapView, BaiduMap mBaiduMap, LocationClient mLocationClient,
                              TextView tv_Lat, TextView tv_Lon, TextView tv_Add,LocationClientOption option) {
        this.mMapView = mMapView;
        this.mBaiduMap = mBaiduMap;
        this.mLocationClient=mLocationClient;
        this.tv_Lat=tv_Lat;
        this.tv_Lon=tv_Lon;
        this.tv_Add=tv_Add;
        this.option=option;
    }

    @Override
    public void onReceiveLocation(BDLocation location) {
        //mapView 销毁后不在处理新接收的位置
        if (location == null || mMapView == null){
            return;
        }
        tv_Lat.setText(location.getLatitude()+"");
        tv_Lon.setText(location.getLongitude()+"");
        tv_Add.setText(location.getAddrStr());

        if(location.getLocType()==BDLocation.TypeGpsLocation || location.getLocType()==BDLocation.TypeNetWorkLocation){
            navigateTo(location);
        }

        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(location.getRadius())
                // 此处设置开发者获取到的方向信息,顺时针0-360
                .direction(location.getDirection()).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();

        mBaiduMap.setMyLocationData(locData);

    }

    private void navigateTo(BDLocation location) {
        if(isFirstLocate){
            LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
            mBaiduMap.animateMapStatus(update);
            isFirstLocate = false;
        }
    }
}

5.通过LocationClient发起定位

//定位初始化
        mLocationClient = new LocationClient(this);
        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        //option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        //设置定位模式,三选一
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //设置需要地址信息
        option.setIsNeedAddress(true);

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

        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener(mMapView,mBaiduMap,
                mLocationClient,tv_Lat,tv_Lon,tv_Add,option);
        mLocationClient.registerLocationListener(myLocationListener);
        //开启地图定位图层
        mLocationClient.start();

6. 正确管理各部分的生命周期

@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();
    }

五、结果截图

在这里插入图片描述
在这里插入图片描述

六、心得

地图显示可以在虚拟机上显示,而定位需要在真机上运行。跟着开发文档做,总体还是很顺利的,但是遇到了一个小问题,最后不断调试通过传值总算解决了。

七、源码

百度地图定位

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值