百度地图退出后会重启一次

Heatherhh 2016-11-30 10:07:46
我在onDestroy()方法中有销毁图层,按返回退出不知道为什么它会跳到onCreat()重新启动一次,再按返回键才会跳出百度地图界面。贴上我的代码
public class Map extends ActivityBasic {

private MapView mMapView;
private ImageButton requestLocButton;
private double Lantitude;// 纬度
private double Longitude;// 经度

// 定位相关
BaiduMap mBaiduMap;
LocationClient mLocClient;
public MyLocationListenner myListener;
boolean isFirstLoc = true; // 是否首次定位
private String mCurrentaddrstr;// 地图标记位置

private MyOnMapClickListener mOnMapClickListener;// 用户点击地图时的回调接口


// 覆盖物
private BitmapDescriptor mMarker;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_location);

initView();

}

private void initView() {
mMapView = (MapView) findViewById(R.id.bmapView);
requestLocButton = (ImageButton) findViewById(R.id.ib_loc);

// 地图初始化
mBaiduMap = mMapView.getMap();
// 设置marker图标
mMarker = BitmapDescriptorFactory.fromResource(R.drawable.maker);
// 设置地图的点击事件
mOnMapClickListener = new MyOnMapClickListener();
mBaiduMap.setOnMapClickListener(mOnMapClickListener);

// 定位初始化
mLocClient = new LocationClient(this);
myListener = new MyLocationListenner();
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);//可选,默认false,设置是否使用gps
mLocClient.setLocOption(option);
mLocClient.start();
}

public class MyOnMapClickListener implements BaiduMap.OnMapClickListener {

@Override
public void onMapClick(LatLng latLng) {
//先清除图层
mBaiduMap.clear();

//获取覆盖物经纬度
Lantitude = latLng.latitude;
Longitude = latLng.longitude;

// 定义Maker坐标点
LatLng point = new LatLng(Lantitude, Longitude);
// 构建MarkerOption,用于在地图上添加Marker
MarkerOptions options = new MarkerOptions().position(point)
.icon(mMarker);
// 在地图上添加Marker,并显示
mBaiduMap.addOverlay(options);

final GeoCoder geoCoder = GeoCoder.newInstance();
//设置反地理编码位置坐标
ReverseGeoCodeOption op = new ReverseGeoCodeOption();
op.location(point);
//发起反地理编码请求(经纬度->地址信息)
geoCoder.reverseGeoCode(op);
geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {


@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
//获取点击的坐标地址
mCurrentaddrstr = arg0.getAddress();
}

@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {

}

});
}

@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}

}


/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {

@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360.direction(100)
.latitude(location.getLatitude())
.longitude(location.getLongitude()).build();

mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
Lantitude = location.getLatitude();
Longitude = location.getLongitude();
mCurrentaddrstr = location.getAddrStr();
}

}

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

@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}

@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}

@Override
protected void onDestroy() {
super.onDestroy();
// 退出时销毁定位
if (mLocClient != null && mLocClient.isStarted()) {
mLocClient.unRegisterLocationListener(myListener);
mLocClient.stop();
mLocClient = null;
}

// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();

}

}
...全文
231 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35101306 2016-12-01
  • 打赏
  • 举报
回复
棒棒哒~ 已私信~
Heatherhh 2016-11-30
  • 打赏
  • 举报
回复
来个人吧~~ help~~~~~~~
Heatherhh 2016-11-30
  • 打赏
  • 举报
回复
不要沉!!!

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧