2 flydragon111 flydragon111 于 2016.05.11 14:33 提问

求大师解答?百度地图开发无法定位到我的位置

这是我的代码:

public class MainActivity extends ActionBarActivity {
private MapView mMapView;
private BaiduMap mBaiduMap;

private LocationClient mLocationClient;
private MyLocatonListener mLocatonListener;
private boolean isFirstIn=true;
private Context context;
private double mLatitude;
private double mLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
  //在使用SDK各组件之前初始化context信息,传入ApplicationContext  
    //注意该方法要再setContentView方法之前实现  
    SDKInitializer.initialize(getApplicationContext());
    setContentView(R.layout.activity_main);

    this.context=this;
    initView();
    initLocation();
}
private void initLocation() {
    // TODO Auto-generated method stub
    //mLocationClient=new LocationClient(this);
    mLocationClient = new LocationClient(getApplicationContext()); 
    mLocatonListener=new MyLocatonListener();
    mLocationClient.registerLocationListener(mLocatonListener);
    LocationClientOption option=new LocationClientOption();
    option.setCoorType("bd09ll");
    option.setIsNeedAddress(true);
    option.setOpenGps(true);
    option.setScanSpan(1000);
    mLocationClient.setLocOption(option);
}


private void initView() {
    // TODO Auto-generated method stub
    mMapView=(MapView) findViewById(R.id.id_bmapView);
    mBaiduMap=mMapView.getMap();

    MapStatusUpdate msu=MapStatusUpdateFactory.zoomTo(15.0f);
    mBaiduMap.animateMapStatus(msu);

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理  
    mMapView.onResume();
}
@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    mBaiduMap.setMyLocationEnabled(true);
    if(mLocationClient.isStarted()){
        mLocationClient.start();

    }
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
     //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理  
    mMapView.onPause();  

}
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    mBaiduMap.setMyLocationEnabled(false);
    mLocationClient.stop();

}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
     //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理  
    mMapView.onDestroy();  

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
    case R.id.id_map_common:
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        break;
    case R.id.id_map_site:
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
        break;
    case R.id.id_map_traffic:
        if(mBaiduMap.isTrafficEnabled()){
            mBaiduMap.setTrafficEnabled(false);
            item.setTitle("实时交通(off)");
        }
        else{
            mBaiduMap.setTrafficEnabled(true);
            item.setTitle("实时交通(on)");
        }
        break;
    case R.id.id_map_location:
        centerToMyLocation();
        break;

    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}


private void centerToMyLocation() {
    LatLng latLng=new LatLng(mLatitude, mLongitude);
    MapStatusUpdate msu=MapStatusUpdateFactory.newLatLng(latLng);
    mBaiduMap.animateMapStatus(msu);
}
public class MyLocatonListener implements BDLocationListener{

    @Override
    public void onReceiveLocation(BDLocation location) {
        // TODO Auto-generated method stub



        MyLocationData data=new MyLocationData.Builder()//
        .accuracy(location.getRadius())//
        .latitude(location.getLatitude())//
        .longitude(location.getLongitude())//
        .build();

        mBaiduMap.setMyLocationData(data);

        mLatitude=location.getLatitude();
        mLongitude=location.getLongitude();

        if(isFirstIn){
            LatLng latLng=new LatLng(location.getLatitude(), location.getLongitude());
            MapStatusUpdate msu=MapStatusUpdateFactory.newLatLng(latLng);
            mBaiduMap.animateMapStatus(msu);
            isFirstIn=false;
            Toast.makeText(context, location.getAddrStr(), Toast.LENGTH_SHORT).show();
        }
    }

}

}

1个回答

flydragon111
flydragon111   2016.05.12 15:16

没有哪个大神知道???图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!