在res 下的 main 新建文件夹 导入所需要的jar包,最后一个添加到小奶瓶
sourceSets { main { jniLibs.srcDir 'libs' //说明so的路径为该libs路径,关联所有地图SDK的so文件 } }
//获取设备网络状态,禁用后无法获取网络状态 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> //网络权限,当禁用后,无法进行检索等相关业务 <uses-permission android:name="android.permission.INTERNET" /> //读取设备硬件信息,统计数据 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> //读取系统信息,包含系统版本等信息,用作统计 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> //获取设备的网络状态,鉴权所需网络代理 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> //允许sd卡写权限,需写入地图数据,禁用后无法显示地图 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //允许sd卡读权限,需读取地图数据,禁用后无法显示地图 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> //获取统计数据 <uses-permission android:name="android.permission.WRITE_SETTINGS" /> //鉴权所需该权限获取进程列表 <uses-permission android:name="android.permission.GET_TASKS" /> //使用步行AR导航,配置Camera权限 <uses-permission android:name="android.permission.CAMERA" /> //网络定位 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> //GPS定位 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
http://lbsyun.baidu.com/apiconsole/key 创建应用
获取SHA1值参考:https://blog.csdn.net/dreamlivemeng/article/details/74378037
作者本机的SHA1: 3E:E7:22:4B:34:44:02:18:84:E6:40:A6:40:AF:E2:FF:06:EC:BA:8A
得到Key 填到meta中
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="" /> 集成百度地图的Key
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" />
XML布局:
xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.com.yk0702_demo.view.MapActivity"> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/bt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="复位" /> <Button android:id="@+id/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="卫星" /> <Button android:id="@+id/buttons" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="普通" /> LinearLayout> RelativeLayout>
/* 地图的Activity */
public class MapActivity extends AppCompatActivity implements View.OnClickListener { private MapView mMapView; private BaiduMap mBaiduMap; public LocationClient mLocationClient; public BDLocationListener myListener = new MyLocationListener(); private Button bt; private Button button; private Button buttons; private LatLng latLng; private boolean isFirstLoc = true; // 是否首次定位 /** * 复位 */ private Button mBt; /** * 卫星 */ private Button mButton; /** * 普通 */ private Button mButtons; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); initView(); initMap(); } private void initMap() { //获取地图控件引用 mBaiduMap = mMapView.getMap(); //普通地图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); mBaiduMap.setMyLocationEnabled(true); //默认显示普通地图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); //开启交通图 //mBaiduMap.setTrafficEnabled(true); //开启热力图 //mBaiduMap.setBaiduHeatMapEnabled(true); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 //配置定位SDK参数 initLocation(); mLocationClient.registerLocationListener(myListener); //注册监听函数 //开启定位 mLocationClient.start(); //图片点击事件,回到定位点 mLocationClient.requestLocation(); } private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span = 1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation // .getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false); option.setOpenGps(true); // 打开gps //可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要 mLocationClient.setLocOption(option); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.bt: //把定位点再次显现出来 MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng); mBaiduMap.animateMapStatus(mapStatusUpdate); break; case R.id.button: //卫星地图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; case R.id.buttons: //普通地图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); break; } } //实现BDLocationListener接口,BDLocationListener为结果监听接口,异步获取定位结果 public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { latLng = new LatLng(location.getLatitude(), location.getLongitude()); // 构造定位数据 MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); // 设置定位数据 mBaiduMap.setMyLocationData(locData); // 当不需要定位图层时关闭定位图层 //mBaiduMap.setMyLocationEnabled(false); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); if (location.getLocType() == BDLocation.TypeGpsLocation) { // GPS定位结果 Toast.makeText(MapActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { // 网络定位结果 Toast.makeText(MapActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) { // 离线定位结果 Toast.makeText(MapActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeServerError) { Toast.makeText(MapActivity.this, "服务器错误,请检查", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { Toast.makeText(MapActivity.this, "网络错误,请检查", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { Toast.makeText(MapActivity.this, "手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show(); } } } } private void initView() { mMapView = (MapView) findViewById(R.id.bmapView); mMapView.setOnClickListener(this); mBt = (Button) findViewById(R.id.bt); mBt.setOnClickListener(this); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(this); mButtons = (Button) findViewById(R.id.buttons); mButtons.setOnClickListener(this); } @Override protected void onDestroy() { super.onDestroy(); mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } }
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> service>
如定位出现蓝屏是动态权限问题、需要在手机上手动设置开启位置权限(模拟器会出现蓝屏)
核实dependencies中的依赖:
implementation files('src/main/jniLibs/BaiduLBS_Android.jar')