1.集成百度地图SDK时,遇到了好几个问题?
1集成后,点击闪退报错是因为,没有同意隐私政策,造成了闪退
解决方法:需要在setContentView之前实现这两个方法
SDKInitializer.setAgreePrivacy(getApplicationContext(), true);
SDKInitializer.initialize(getApplicationContext());
2.集成地图功能,没有出现定位小蓝点的原因
是对Api不熟悉,在使用MapStatusUpdate方法 实现地图zoomTo的方法,造成了
mBaiduMap.animateMapStatus(update);
这个方法失效,导致没有定位到当前为止显示小蓝点
作为小白,也是花了大量的时间在百度上,而没有研究API使用方法,在出现问题时,切记要先看方法本身,实在解决不了的在进行百度
以下是完整实现
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: 百度地图SDK初始化");
SDKInitializer.setAgreePrivacy(getApplicationContext(), true);
SDKInitializer.initialize(getApplicationContext());
initLoc();
setContentView(R.layout.activity_lbs_map);
locText = (TextView) findViewById(R.id.position_text_view);
mapView = (MapView)findViewById(R.id.map_view);
mBaiduMap = mapView.getMap();
mBaiduMap.setMyLocationEnabled(true);
checkPermission();
}
private void checkPermission() {
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(BDMapActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(BDMapActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(BDMapActivity.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(BDMapActivity.this, permissions, 1);
}else {
requestLocation();
}
}
private void initLoc() {
LocationClient.setAgreePrivacy(true);
// 定位初始化
try {
mLocationClient = new LocationClient(this);
} catch (Exception e) {
e.printStackTrace();
}
if (mLocationClient != null){
Log.d(TAG, "initLoc: 地图注册监听事件");
mLocationClient.registerLocationListener(new MyLocationListener());
}
}
private void requestLocation() {
Log.d(TAG, "requestLocation: 开始定位");
initLocation();
}
private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setScanSpan(5000);// 5秒更新一次位置信息
option.setIsNeedAddress(true); // 返回地址信息
option.setOpenGps(true);
option.setCoorType("bd09ll");
mLocationClient.setLocOption(option);
// 开启地图定位图层
mLocationClient.start();
Log.d(TAG, "initLocation: 开启定位图层");
}
@SuppressLint("MissingSuperCall")
@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(this, "必须同意所有权限才能使用本程序", Toast.LENGTH_SHORT).show();
finish();
return;
}
requestLocation();
}
} else {
Toast.makeText(this, "发生未知错误", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
@Override
protected void onResume() {
mapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
mapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
mLocationClient.stop();
mBaiduMap.setMyLocationEnabled(false);
mapView.onDestroy();
mapView = null;
super.onDestroy();
}
public class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null || mapView == null) {
return;
}
Log.d(TAG, "当前线程: " + Thread.currentThread());
StringBuilder builder = new StringBuilder();
Log.d(TAG, "run: " + "纬度: " + location.getLatitude() + "定位方式: " + location.getLocType() );
builder.append("纬度: ").append(location.getLatitude()).append("\n");
builder.append("经度: ").append(location.getLongitude()).append("\n");
builder.append("国家: ").append(location.getCountry()).append("\n");
builder.append("省: ").append(location.getProvince()).append("\n");
builder.append("市: ").append(location.getCity()).append("\n");
builder.append("区: ").append(location.getDistrict()).append("\n");
builder.append("街道: ").append(location.getStreet()).append("\n");
builder.append("定位方式: ");
if (location.getLocType() == BDLocation.TypeGpsLocation) {
builder.append("GPS");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
builder.append("网络");
}
locText.setText(builder);
if (isFirstLocate) {
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll, 18f);
// update = MapStatusUpdateFactory.zoomTo(18f);
mBaiduMap.animateMapStatus(update);
isFirstLocate = false;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
.direction(location.getDirection())
.latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
}
}
希望能帮助后来人