Android百度地图定位有问题,没有图层显示,请大神帮看下

public class MainActivity extends AppCompatActivity {
private MapView mMapView = null;
private BaiduMap mBaiduMap;
private LocationClient mLocationClient;
private BDLocationListener myLocationListener = new MyLocationListener();
private boolean isFirstIn = true;
private double mLantitude;
private double mLongtitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    initView();
    initLocation();
}

private void initLocation() {
    mBaiduMap.setMyLocationEnabled(true);
    mLocationClient = new LocationClient(this);
    mLocationClient.registerLocationListener(myLocationListener);
    LocationClientOption option = new LocationClientOption();
    option.setCoorType("bd09ll");
    option.setOpenGps(true);
    option.setIsNeedAddress(true);
    option.setScanSpan(1000);
    mLocationClient.setLocOption(option);
    mLocationClient.start();
}

private void initView() {
    mMapView = (MapView) findViewById(R.id.bmapView);
    mBaiduMap = mMapView.getMap();
    MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
    mBaiduMap.setMapStatus(msu);
}

@Override
protected void onStart() {
    mBaiduMap.setMyLocationEnabled(true);
    if (!mLocationClient.isStarted()){
        mLocationClient.start();
    }
    super.onStart();


}


@Override
protected void onDestroy() {
    super.onDestroy();
    //在activity执行onDestroy时执行mMapView.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();
}

@Override
protected void onStop() {
    mBaiduMap.setMyLocationEnabled(false);
    mLocationClient.stop();
    super.onStop();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.menu_common:
            mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
            break;
        case R.id.menu_site:
            mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
            break;
        case R.id.menu_traffic:
            if (mBaiduMap.isTrafficEnabled()){
                mBaiduMap.setTrafficEnabled(false);
                item.setTitle("交通地图(off)");
            }else {
                mBaiduMap.setTrafficEnabled(true);
                item.setTitle("交通地图(on)");
            }
            break;
        case R.id.menu_location:
            LatLng latLng = new LatLng(mLantitude,mLongtitude);
            MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
            mBaiduMap.animateMapStatus(msu);
            break;

    }
    return super.onOptionsItemSelected(item);
}
private class MyLocationListener implements BDLocationListener{

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        MyLocationData data = new MyLocationData.Builder()
                .accuracy(bdLocation.getRadius())
                .latitude(bdLocation.getLatitude())
                .longitude(bdLocation.getLongitude())
                .build();

        mBaiduMap.setMyLocationData(data);
        if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
            Log.e("eeeeeeeeeee","定位成共");
        }else if(bdLocation.getLocType() == BDLocation.TypeNetWorkLocation){
            Log.e("eeeeeeeeeee","网络定位成共");
        }else if(bdLocation.getLocType() == BDLocation.TypeServerError){
            Log.e("eeeeeeeeeee","服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
        }else if(bdLocation.getLocType() == BDLocation.TypeNetWorkException){
            Log.e("eeeeeeeeeee","网络不同导致定位失败,请检查网络是否通畅");
        }else if (bdLocation.getLocType() == BDLocation.TypeCriteriaException){
            Log.e("eeeeeeeeeee","无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
        }

        if (isFirstIn){
            mLantitude = bdLocation.getLatitude();
            mLongtitude = bdLocation.getLongitude();
            LatLng latLng = new LatLng(mLantitude,mLongtitude);
            MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
            mBaiduMap.animateMapStatus(msu);
            isFirstIn = false;
        }

    }
}

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐