android百度地图开发——定位sdk
来源:互联网 发布:炎亚纶汪东城天涯 知乎 编辑:程序博客网 时间:2024/06/06 10:01
上一节我们介绍了android studio中配置百度地图sdk,这一节我们做一个定位的实例:
首先我们在布局文件中添加一个textview:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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.lbstest.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /></android.support.constraint.ConstraintLayout>
接着我们在Activity的oncreate()方法中创建一个LocationClient实例,注意LocationClient的实例化要放在setContentView()之前,实例化后对LocationClient设置监听:
public TextView positionText; public LocationClient mlocation; private List<String> permissionList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mlocation = new LocationClient(getApplicationContext()); //注册监听事件 mlocation.registerLocationListener(new MyLocationListener()); setContentView(R.layout.activity_main); positionText = (TextView) findViewById(R.id.textView); //申请权限 initPermission(); }
还记得我们在AndroidManifest.xml中添加的权限吗,其中有一些是敏感权限,在android6.0中需要动态申请,下面介绍一种同时申请多个权限的方法:
List<String> permissionList = new ArrayList<>(); if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); } if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.READ_PHONE_STATE); } if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (!permissionList.isEmpty()) { String[] permissions = permissionList.toArray(new String[permissionList.size()]); ActivityCompat.requestPermissions(MainActivity.this, permissions, 1); } else { requestLocation(); }
这里创建一个list集合,然后依次判断这三个权限有没有被授权,如果没授权就添加到list集合中,最后将list集合转换成数组,在一次性申请。
申请的回调:
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { Toast.makeText(MainActivity.this, "必须同意所以权限", Toast.LENGTH_LONG).show(); finish(); return; } } requestLocation(); } else { finish(); } break; default: } }
当全部权限都授权后调用requestLocation()方法,在start后开始调用监听回调:
private void requestLocation() { initLocation(); mlocation.start(); } private void initLocation() { LocationClientOption option = new LocationClientOption(); //可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); //可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setScanSpan(5000); //设置百度坐标 option.setCoorType("bd09ll"); option.setOpenGps(true); // 打开gps //可选,设置是否需要地址信息,默认不需要 option.setIsNeedAddress(true); mlocation.setLocOption(option); }
这里说一下定位mode:
- Hight_Accuracy:表示高精度模式,优先使用GPS,无法使用GPS的时候使用网络定位。
- Battery_Saving:节电模式,只是用网络定位。
- Device_Sensors:传感器模式,只是用GPS定位。
LocationClient的监听:
public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation bdLocation) { Log.i("info", "listener"); final StringBuilder currentPosition = new StringBuilder(); currentPosition.append("经度:").append(bdLocation.getLatitude()).append("\n"); currentPosition.append("纬度:").append(bdLocation.getLongitude()).append("\n"); currentPosition.append("省:").append(bdLocation.getProvince()).append("\n"); currentPosition.append("市:").append(bdLocation.getCity()).append("\n"); currentPosition.append("区:").append(bdLocation.getDistrict()).append("\n"); currentPosition.append("街道:").append(bdLocation.getStreet()).append("\n"); currentPosition.append("定位方式:"); if (bdLocation.getLocType()== BDLocation.TypeGpsLocation) { currentPosition.append("gps"); } else if (bdLocation.getLocType()==BDLocation.TypeNetWorkLocation) { currentPosition.append("net"); } Log.i("info", currentPosition.toString()); runOnUiThread(new Runnable() { @Override public void run() { positionText.setText(currentPosition); } }); } @Override public void onConnectHotSpotMessage(String s, int i) { } }
填坑:监听方法是实现在子线程中的,在这里不能做更新ui的操作。
最后在Activity的onDestroy()方法中调用stop函数:
@Override protected void onDestroy() { super.onDestroy(); mlocation.stop(); }
填坑:我们把定位方式设置为高精度,这样当开启GPS的时候就会使用GPS定位,但是使用GPS的前提是在室外,否则即使开了GPS 也不能用。
运行图:
完整代码:
public class MainActivity extends AppCompatActivity { public TextView positionText; public LocationClient mlocation; private List<String> permissionList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mlocation = new LocationClient(getApplicationContext()); mlocation.registerLocationListener(new MyLocationListener()); setContentView(R.layout.activity_main); positionText = (TextView) findViewById(R.id.textView); initPermission(); } private void initPermission() { permissionList = new ArrayList<>(); if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); } if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.READ_PHONE_STATE); } if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (!permissionList.isEmpty()) { String[] permissions = permissionList.toArray(new String[permissionList.size()]); ActivityCompat.requestPermissions(MainActivity.this, permissions, 1); } else { requestLocation(); } } private void requestLocation() { initLocation(); mlocation.start(); } private void initLocation() { LocationClientOption option = new LocationClientOption(); //可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); //可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setScanSpan(5000); //可选,设置是否需要地址信息,默认不需要 option.setIsNeedAddress(true); mlocation.setLocOption(option); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { Toast.makeText(MainActivity.this, "必须同意所以权限", Toast.LENGTH_LONG).show(); finish(); return; } } requestLocation(); } else { finish(); } break; default: } } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation bdLocation) { Log.i("info", "listener"); final StringBuilder currentPosition = new StringBuilder(); currentPosition.append("经度:").append(bdLocation.getLatitude()).append("\n"); currentPosition.append("纬度:").append(bdLocation.getLongitude()).append("\n"); currentPosition.append("省:").append(bdLocation.getProvince()).append("\n"); currentPosition.append("市:").append(bdLocation.getCity()).append("\n"); currentPosition.append("区:").append(bdLocation.getDistrict()).append("\n"); currentPosition.append("街道:").append(bdLocation.getStreet()).append("\n"); currentPosition.append("定位方式:"); if (bdLocation.getLocType()== BDLocation.TypeGpsLocation) { currentPosition.append("gps"); } else if (bdLocation.getLocType()==BDLocation.TypeNetWorkLocation) { currentPosition.append("net"); } Log.i("info", currentPosition.toString()); runOnUiThread(new Runnable() { @Override public void run() { positionText.setText(currentPosition); } }); } @Override public void onConnectHotSpotMessage(String s, int i) { } } @Override protected void onDestroy() { super.onDestroy(); mlocation.stop(); }}
有兴趣的小伙伴看我的另一篇文章android地图开发——地图sdk
2 0
- android百度地图开发——定位sdk
- 百度地图Android sdk开发笔记四sdk定位
- Android 百度地图SDK定位
- 百度地图 Android定位SDK--【显示地图 】
- 百度地图 Android 定位SDK 开发指南4.2
- android百度地图开发——android studio配置sdk
- 百度地图Android SDK 2.0开发指南2-结合百度定位SDK在百度地图上显示当前位置
- Android版本百度地图开发(四)——定位
- Android版本百度地图开发(四)——定位
- 百度地图SDK for Android【定位】
- 百度地图Android定位SDK的API
- Android--定位 基于百度地图SDK
- android 百度地图定位SDK使用方法
- Android 百度定位及地图SDK
- 百度地图移动开发——(Android SDK v3.5.0)
- Android百度地图SDK开发
- 百度SDK地图定位
- 定位 -- 百度地图SDK
- [Java EE] 在JavaServer Faces中使用HTML5中的日期控件
- 遍历二叉树
- HDU1108最小公倍数 水题
- 单例模式(总结)
- HDU4081 Qin Shi Huang's National Road System
- android百度地图开发——定位sdk
- css:默认的checkbox、input、radio太丑了?手把手教你改变使用纯css3改写的带动画的默认样式
- Winfrom异步更新UI
- 【动态规划】Leetcode编程题解:121. Best Time to Buy and Sell Stock Add to List
- [UE4](译)(Rama)保存系统,二进制压缩文件读写信息
- JAVA学习第2天 面向对象相关概念
- 【XML】——XML文档增删改查
- JVM内存管理垃圾收集器GC
- SparkSQL之JSON