Android Fragment集成高德地图黑屏的问题解决方案

来源:互联网 发布:c语言fact函数 编辑:程序博客网 时间:2024/04/29 02:20

官方给的3.0Map的集成api说了:


MapView 是 Android View 类的一个子类,它可以帮助您在 Android View 中放置地图,它是应用程序和窗口部件的基本构建类。MapView 作为地图的容器,通过 AMap 对象显示地图。使用 MapView 类,必须重载 Activity 生命周期的所有方法,有 onCreate(),onDestroy(),onResume(),onPause(),onSaveInstanceState()。


另外,如果是在Fragment中集成MapView的话,首先需要将你自己写的Fragment继承SupportMapFragment类,然后同样需要重载所有的生命周期方法。区别在于:


Fragment的onCreate 方法再onCreateView方法之前调用,而Fragment的视图是在onCreateView中初始化的,所以这里需要将MapView的onCreatef(Bundle)方法移到onCreateView方法中进行初始化。


但是,我前期做的时候犯了一个错误,MapView的其他生命周期方法调用位置为都没改,结果app首次进入的时候,高德地图是正常加载的,但是退出后重新进入,app会一直处于黑屏的位置。


后来发现是因为MapView.onDestroy方法调用不对,应该移到Fragment的onDestroyView中,修改后,问题解决。


只能说对Fragment的了解还不是很充足,官方对Fragment中的调用区别也没有明确说明出来,浪费了一天时间。


最后附上源码:

<pre name="code" class="java">public class MapFragment extends SupportMapFragment implementsAMapLocationListener, LocationSource {private String TAG = this.getClass().getSimpleName();private int fragmentId;private MapView mMapView;private AMap mAmap;// 声明AMapLocationClient类对象private AMapLocationClient mLocationClient;private AMapLocationClientOption mLocationOption;// 声明定位回调监听器public AMapLocationListener mLocationListener;private AMapLocation mMyLocationPoint;// 我的位置监听器private OnLocationChangedListener mLocationChangeListener = null;private View mRoot;// private@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {mRoot = inflater.inflate(R.layout.fragment_friends, container,false);mMapView = (MapView) mRoot.findViewById(R.id.amap);mMapView.onCreate(savedInstanceState);initMap();Logs.d(TAG, "onCreateView");return mRoot;}private void initMap() {if (mAmap == null) {mAmap = mMapView.getMap();initMyLocation();}}/** * 初始化定位服务 */private void initLocation() {mLocationClient = new AMapLocationClient(getActivity());mLocationClient.setLocationListener(this);// 初始化定位参数mLocationOption = new AMapLocationClientOption();// 设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);// 设置是否返回地址信息(默认返回地址信息)mLocationOption.setNeedAddress(true);// 设置是否只定位一次,默认为falsemLocationOption.setOnceLocation(false);// 设置是否强制刷新WIFI,默认为强制刷新mLocationOption.setWifiActiveScan(true);// 设置是否允许模拟位置,默认为false,不允许模拟位置mLocationOption.setMockEnable(false);// 设置定位间隔,单位毫秒,默认为2000msmLocationOption.setInterval(2000);// 给定位客户端对象设置定位参数mLocationClient.setLocationOption(mLocationOption);// 启动定位mLocationClient.startLocation();}/** * 初始化我的定位 */private void initMyLocation() {mAmap.setLocationSource(this);mAmap.getUiSettings().setMyLocationButtonEnabled(true);mAmap.setMyLocationEnabled(true);mAmap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);mAmap.getUiSettings().setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);// logo位置mAmap.getUiSettings().setScaleControlsEnabled(true);// 标尺开关mAmap.getUiSettings().setCompassEnabled(true);// 指南针开关Log.d(TAG,"max = " + mAmap.getMaxZoomLevel() + "min = "+ mAmap.getMinZoomLevel());}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();mMapView.onPause();if(mLocationClient!=null){mLocationClient.stopLocation();//ֹͣ��λ}deactivate();}@Overridepublic void onResume() {// TODO Auto-generated method stubsuper.onResume();mMapView.onResume();if(mLocationClient!=null){mLocationClient.startLocation();}mAmap.moveCamera(CameraUpdateFactory.zoomTo(14));}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}@Overridepublic void onSaveInstanceState(Bundle arg0) {// TODO Auto-generated method stubsuper.onSaveInstanceState(arg0);mMapView.onSaveInstanceState(arg0);}@Overridepublic void onDestroyView() {// TODO Auto-generated method stubsuper.onDestroyView();if(mLocationClient!=null){mLocationClient.onDestroy();}mMapView.onDestroy();}@Overridepublic void deactivate() {Log.d(TAG, "deactivate");mLocationChangeListener = null;if (mLocationClient != null) {mLocationClient.stopLocation();mLocationClient.onDestroy();mLocationClient = null;}}@Overridepublic void activate(OnLocationChangedListener listener) {Log.d(TAG, "activate");mLocationChangeListener = listener;if (mLocationClient == null) {initLocation();}}@Overridepublic void onLocationChanged(AMapLocation amapLocation) {Log.d(TAG, "onLocationChanged");if (amapLocation != null) {if (amapLocation.getErrorCode() == 0) {// 定位成功回调信息,设置相关消息amapLocation.getLocationType();// 获取当前定位结果来源,如网络定位结果,详见定位类型表amapLocation.getLatitude();// 获取经度amapLocation.getLongitude();// 获取纬度amapLocation.getAccuracy();// 获取精度信息SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(amapLocation.getTime());df.format(date);// 定位时间amapLocation.getAddress();// 地址,如果option中设置isNeedAddress为false,则没有此结果amapLocation.getCountry();// 国家信息amapLocation.getProvince();// 省信息amapLocation.getCity();// 城市信息amapLocation.getDistrict();// 城区信息amapLocation.getRoad();// 街道信息amapLocation.getCityCode();// 城市编码amapLocation.getAdCode();// 地区编码mMyLocationPoint = amapLocation;mLocationChangeListener.onLocationChanged(mMyLocationPoint);} else {// 显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。Log.e("AmapError","location Error, ErrCode:"+ amapLocation.getErrorCode() + ", errInfo:"+ amapLocation.getErrorInfo());}}}}


0 1
原创粉丝点击