Android Studio 3.2,JDK1.8 运行Android 7.1系统(需要权限申请)
https://developer.baidu.com/
http://lbsyun.baidu.com/ ➡登陆➡控制台➡创建应用➡提交
http://lbsyun.baidu.com/index.php?title=androidsdk/sdkandev-download
解压下载的文件 ➡ 进入 libs
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List permissionlist = new ArrayList<>();
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
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.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.ACCESS_WIFI_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.ACCESS_NETWORK_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.CHANGE_WIFI_STATE);
}
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 (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.INTERNET);
}
if (!permissionlist.isEmpty()) {
String[] permissions = permissionlist.toArray(new String[permissionlist.size()]);
ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
} else {
initData();
}
}
@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_SHORT).show();
finish();
return;
}
}
initData();
} else {
Toast.makeText(MainActivity.this, "发生未知错误", Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
private void initData() {
//找到控件
showData = findViewById(R.id.show_data);
//设置位置客户端选项
LocationClientOption option = new LocationClientOption();
//设置位置取得模式 (这里是指定为设备传感器也就是 GPS 定位)
option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
//设置 间隔扫描的时间 也就是 位置时隔多长时间更新
option.setScanSpan(5000);
//设置 是否需要地址 (需要联网取得 百度提供的位置信息)
option.setIsNeedAddress(true);
// 实例化 LocationClient 传入的context 应该是全局的
mLocationListener = new LocationClient(getApplicationContext());
//将选项设置进去
mLocationListener.setLocOption(option);
//设置监听器 (这个方法有两个其中一个过时了,要使用 new BDAbstractLocationListener 的这个监听器)
mLocationListener.registerLocationListener(new BDAbstractLocationListener() {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuilder currentPosition = new StringBuilder();
//获取经纬度
currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("经线:").append(bdLocation.getLongitude()).append("\n");
//获取详细地址信息
currentPosition.append("国家:").append(bdLocation.getCountry()).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("网络 定位");
}
showData.setText(currentPosition);
}
});
//显示到Activity
mLocationListener.start();
}
如果不连接网络 ,是不会显示地址信息的,地址的显示需要网络
手机GPS连接卫星是需要时间的 ,而且最好在户外
//设置位置取得模式 (这里是指定为设备传感器也就是 GPS 定位)
option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
如果设置了位置取得模式为 GPS (如上代码):
联网状态 :GPS选项是 高精度 和 仅限设备 是没有区别的 都会先显示网络方式然后显示GPS方式
选择 低耗电量 就只能显示网络方式
无网络状态:位置的地址信息是不会显示的 ,GPS选项是 高精度 和 仅限设备 是没有区别的, 都
会显示GPS方式 选择 低耗电量 就什么变化都没有
如果没有设置位置取得模式为 GPS:
不管GPS的选项是什么,它获取位置的模式都是网路,没有网路什么都没有,有网路全部都显示
不需要变动清单文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//只是添加了这一行代码
//一定要在加载布局之前
//作用: 进行初始化
SDKInitializer.initialize(getApplicationContext());
//在这前面
setContentView(R.layout.activity_main);
List permissionlist = new ArrayList<>();
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
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.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.ACCESS_WIFI_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.ACCESS_NETWORK_STATE);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.CHANGE_WIFI_STATE);
}
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 (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
permissionlist.add(Manifest.permission.INTERNET);
}
if (!permissionlist.isEmpty()) {
String[] permissions = permissionlist.toArray(new String[permissionlist.size()]);
ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
} else {
initData();
}
}
没有变化
private void initData() {
showData = findViewById(R.id.show_data);
//下面三行添加代码
//初始化控件
showBdmap = findViewById(R.id.show_bdmap);
//使用控件得到百度地图实例
Baidumap = showBdmap.getMap();
//百度地图设置是否显示我的位置
Baidumap.setMyLocationEnabled(true);
//设置位置客户端选项
LocationClientOption option = new LocationClientOption();
//设置位置取得模式 (这里是指定为设备传感器也就是 GPS 定位)
//option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
//设置 间隔扫描的时间 也就是 位置时隔多长时间更新
option.setScanSpan(5000);
//设置 是否需要地址 (需要联网取得 百度提供的位置信息)
option.setIsNeedAddress(true);
mLocationListener = new LocationClient(getApplicationContext());
mLocationListener.setLocOption(option);
mLocationListener.registerLocationListener(new BDAbstractLocationListener() {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuilder currentPosition = new StringBuilder();
//获取经纬度
currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("经线:").append(bdLocation.getLongitude()).append("\n");
//获取详细地址信息
currentPosition.append("国家:").append(bdLocation.getCountry()).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("网络 定位");
}
//更新地图位置 (添加代码)
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation ||
bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
navigateTo(bdLocation);
}
showData.setText(currentPosition);
}
});
mLocationListener.start();
}
//更新位置的方法
private void navigateTo(BDLocation location) {
//只是让它更新一次
if (isFirstLocate) {
//获得经纬度 装进 LatLng
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
//设置地图更新位置到 MapStatusUpdate 里面
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
//地图更新
Baidumap.animateMapStatus(update);
//设置缩放比例
update = MapStatusUpdateFactory.zoomTo(16f);
//地图更新
Baidumap.animateMapStatus(update);
//设置旗标为 false 以后不再执行位置更新
isFirstLocate = false;
}
// 构建 我的位置
MyLocationData.Builder builder = new MyLocationData.Builder();
//获取经纬度
builder.latitude(location.getLatitude());
builder.longitude(location.getLongitude());
//构建
MyLocationData locationData = builder.build();
//百度地图设置
Baidumap.setMyLocationData(locationData);
}
/**
* 都是一些资源的释放工作
*/
@Override
protected void onResume() {
super.onResume();
showBdmap.onResume();
}
@Override
protected void onPause() {
super.onPause();
showBdmap.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationListener.stop();
showBdmap.onDestroy();
Baidumap.setMyLocationEnabled(false);
}
}
这个实例只是能看地图,就和买的地图一样,不能导航,定位等等
运行效果: 显示我的位置的小蓝点 和 实际所在的位置有误差