在显示地图的时候没出现问题,使用定位时程序直接运行不了,困扰了好久,第一次写博客,请求大神搭救,代码如下
public class MainActivity extends Activity {
private TextureMapView mMapView = null;
private BaiduMap mBaiduMap;
//定位相关
private LocationClient mLocationClient = null;//定位设置
private MyLocationListener myListener;
private Boolean isFirstIn = true;
private Button b_nowlocation;
@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
//初始化控件
initView();
//初始化定位
initLocation();
mLocationClient.start();
//b_nowlocation = (Button)findViewById(R.id.b_nowLocation);
//b_nowlocation.setOnClickListener(new nowLocationListener());
}
private void initView() {
//获取地图控件引用
mMapView = (TextureMapView) findViewById(R.id.mTexturemap);
//获取百度地图
mBaiduMap = mMapView.getMap();
//普通地图
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
// 设置可改变地图位置
mBaiduMap.setMyLocationEnabled(true);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
mBaiduMap.setMapStatus(msu);//注意调用顺序
//mBaiduMap.setTrafficEnabled(true); //开启交通图
}
@SuppressWarnings("deprecation")
private void initLocation() {
//声明LocationClient类,getApplicationContext()
mLocationClient = new LocationClient(getApplicationContext());
myListener = new MyLocationListener();
mLocationClient.registerLocationListener(myListener); //注册监听函数
LocationClientOption option = new LocationClientOption();
option.setCoorType("bd09ll");//坐标类型
option.setIsNeedAddress(true);//返回当前位置
option.setOpenGps(true);//定位方式
option.setScanSpan(1000);//定位时间间隔
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener{
public void onReceiveLocation(BDLocation location){
//此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果
MyLocationData data = new MyLocationData.Builder()//
.accuracy(location.getRadius())//精度
.latitude(location.getLongitude())//方向
.longitude(location.getLongitude())//
.build();
mBaiduMap.setMyLocationData(data);
//MyLocationConfiguration config = new MyLocationConfiguration(LocationMode.FOLLOWING;, arg1, arg2)
if(isFirstIn){
LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude());//经纬度
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latlng);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
}
}
}
/* 屏幕方向或键盘状态改变时相应处理 函数*/
public void onConfigurationChanged(Configuration newConfig ){
super.onConfigurationChanged(newConfig);
}
protected void onStart(){
super.onStart();
mLocationClient.start();//开始定位
}
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
mBaiduMap.setMyLocationEnabled(false);//不允许定位
mLocationClient.stop();//关闭定位
}
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
}