activitygroup中有四个子activity,默认显示的是地图activity,刚开始初始化时地图activity可以显示,但是在点击其他activity后,返回地图activity,地图不能显示...
注意:我现在用的是百度地图,之前用google地图没有这个问题。
------解决方案--------------------
我是这样解决的,把mapView地图状态保存了,在onResume的时候恢复地图状态,在onPause保存地图状态,自己定义了一个MapInfo进行保存geoPoint,跟zoomLevel;
代码
- Java code
import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Point;import android.graphics.Typeface;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.LocationListener;import com.baidu.mapapi.MapActivity;import com.baidu.mapapi.MapController;import com.baidu.mapapi.MapView;import com.baidu.mapapi.Overlay;import com.baidu.mapapi.Projection;import com.life.hua.activity.BaseApplication;import com.life.hua.activity.R;import com.life.hua.entity.MapInfo;import com.life.hua.util.Configer;import com.life.hua.util.Util;public class MyLocalMapActivity extends MapActivity { LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } private Context mContext; private MapView mapView; BaseApplication baseApp; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); this.mContext = this; baseApp = (BaseApplication) this.getApplication(); if (baseApp.mBMapManage == null) { baseApp.mBMapManage = new BMapManager(mContext); baseApp.mBMapManage.init(baseApp.mStrKey, new BaseApplication.MyGeneralListener()); } else { baseApp.mBMapManage.start(); } setContentView(R.layout.baidumap_layout); mapView = (MapView) findViewById(R.id.bmapView); } private MapInfo mapInfo; private MapController mMapController; MyOverlay myOverlay; @Override protected void onResume() { // 注册定位事件 System.out.println("onResume"); mapView.removeAllViews(); super.initMapActivity(baseApp.mBMapManage); mMapController = mapView.getController(); baseApp.mBMapManage.start(); // 恢复地图状态 if (mapInfo != null) { mMapController.setCenter(mapInfo.getCenter()); mMapController.setZoom(mapInfo.getZoomLevel());// 放大级别 mMapController.animateTo(mapInfo.getCenter()); } else { GeoPoint geoPoint = new GeoPoint((int) (Configer.la * 1e6), (int) (Configer.lo * 1e6)); mMapController.zoomIn();// 放大级别 mMapController.animateTo(geoPoint); } mapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件 mapView.setDrawOverlayWhenZooming(true); myOverlay = new MyOverlay(); mapView.getOverlays().add(myOverlay); super.onResume(); } @Override protected void onPause() { System.out.println("onPause"); // 保存地图状态 if (mapInfo == null) { mapInfo = new MapInfo(); } mapInfo.setCenter(mapView.getMapCenter()); mapInfo.setZoom(mapView.getZoomLevel()); BaseApplication app = (BaseApplication) this.getApplication(); app.mBMapManage.getLocationManager().removeUpdates(mLocationListener); app.mBMapManage.stop(); super.onPause(); } @Override protected void onDestroy() { System.out.println("onDestroy"); super.onDestroy(); BaseApplication app = (BaseApplication) this.getApplication(); app.mBMapManage.getLocationManager().removeUpdates(mLocationListener); mapView.removeAllViews(); app.mBMapManage.stop(); System.out.println("onDestroy"); } // 画自己位置图 class MyOverlay extends Overlay { private Bitmap bgBitmap; public MyOverlay() { bgBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.mylocation); } @Override public void draw(Canvas canvas, MapView mapView, boolean arg2) { super.draw(canvas, mapView, arg2); Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setTypeface(Typeface.MONOSPACE); paint.setTextSize(14); Point point = new Point(); Projection projection = mapView.getProjection(); GeoPoint geoPoint = new GeoPoint((int) (Configer.la * 1e6), (int) (Configer.lo * 1e6)); projection.toPixels(geoPoint, point); int bgWidth = bgBitmap.getWidth(); int bgHeight = bgBitmap.getHeight(); canvas.drawBitmap(bgBitmap, point.x - bgWidth / 2, point.y - bgHeight, paint);// 画背景 canvas.drawText("我在这!", point.x - bgWidth / 2 + 30, point.y - bgHeight / 2, paint); } }