Toast提示位置正确,但是不显示地图,也不显示蓝色按钮
package com.example.mytab_fragment_viewpager;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;
public class myMap extends Activity{
private MapView mMapView ;
private BaiduMap mBaiduMap;
private Context context;
//定位相关
private LocationClient mLocationClient;//定位API,设置是否开启GPS等信息
private MyLocationListener mLocationListener;//定位监听器
private boolean isFirstIn=true;
private double mLatitude;//记录最新经纬度
private double mLongtitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.mymap);
this.context=this;
initView();
//初始化定位
initLocation();
}
private void initLocation() {
// TODO Auto-generated method stub
mLocationClient=new LocationClient(this);
mLocationListener=new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);//进行注册
//对LocationClient进行配置
LocationClientOption option=new LocationClientOption();
option.setCoorType("bd09ll");//坐标类型
option.setIsNeedAddress(true);//返回当前位置
option.setOpenGps(true);
option.setScanSpan(1000);//每隔1秒钟做一次请求
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);//将百度地图设置放大到500米
mBaiduMap.setMapStatus(msu);
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onStart() {
// 开启定位
super.onStart();
mBaiduMap.setMyLocationEnabled(true);//开启定位允许
if(!mLocationClient.isStarted())
mLocationClient.start();//开启定位
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
// mMapView.onPause();
mMapView.onDestroy();
}
@Override
protected void onStop() {
// 关闭定位
super.onStop();
//停止定位
mBaiduMap.setMyLocationEnabled(false);//不允许定位
mLocationClient.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {//添加按钮
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
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("关闭实时交通");
}else{
mBaiduMap.setTrafficEnabled(true);
item.setTitle("打开实时交通");
}
break;
case R.id.id_map_location:
centerToMyLocation();
break;
default:
break;
}
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
/*
* 定位到我的位置
*/
private void centerToMyLocation() {
LatLng latLng=new LatLng(mLatitude, mLongtitude);//获取经纬度
MapStatusUpdate msu=MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(msu);
}
private class MyLocationListener implements BDLocationListener{
@Override
public void onReceiveLocation(BDLocation location) {//定位成功后的回调
// TODO Auto-generated method stub
MyLocationData data=new MyLocationData.Builder()//Builder()模式(当参数比较多时,使用Builder内部类)
.accuracy(location.getRadius())//精度
.latitude(location.getLongitude())//
.longitude(location.getLongitude())//
.build();
mBaiduMap.setMyLocationData(data);
//MyLocationConfiguration config=new MyLocationConfiguration(LocationMode.NORMAL, arg1, arg2);
//获取最新经纬度
mLatitude=location.getLatitude();
mLongtitude=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();
}
}
}
}