zoukankan      html  css  js  c++  java
  • 【Android笔记】Fragment中显示高德地图

    本文来自:fair-jm.iteye.com 转截请注明出处

    官网的教程是在Activity下 在Fragment下在高德论坛找到一些方法 试了下可以显示 但是切换后总会有些问题

    比如切换后就是新的了 切换后地图就不显示了

    我这种方式可以在切换后保持地图状态 但是得限定屏幕为水平或者竖直 如果翻转的话也会报错

    布局文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3   android:layout_width="fill_parent"
     4   android:layout_height="fill_parent" >
     5 
     6   <com.amap.api.maps.MapView
     7     xmlns:android="http://schemas.android.com/apk/res/android"
     8     android:id="@+id/mapView"
     9     android:layout_width="match_parent"
    10     android:layout_height="match_parent" >
    11   </com.amap.api.maps.MapView>
    12 
    13 </RelativeLayout>

    Java代码:

      1 package com.cc.android.map.fragment;
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 import android.support.v4.app.Fragment;
      6 import android.util.Log;
      7 import android.view.LayoutInflater;
      8 import android.view.View;
      9 import android.view.ViewGroup;
     10 
     11 import com.amap.api.maps.AMap;
     12 import com.amap.api.maps.MapView;
     13 import com.cc.android.map.MainActivity;
     14 import com.cc.android.map.R;
     15 import com.cc.android.map.constant.Constants;
     16 
     17 public class MapFragment extends Fragment {
     18   
     19   private static MapFragment fragment=null;
     20   public static final int POSITION=0;
     21   
     22   private MapView mapView;
     23   private AMap aMap;
     24   private View mapLayout;
     25   
     26   public static Fragment newInstance(){
     27     if(fragment==null){
     28       synchronized(MapFragment.class){
     29         if(fragment==null){
     30           fragment=new MapFragment();
     31         }
     32       }
     33     }
     34     return fragment;
     35   }
     36   @Override
     37   public View onCreateView(LayoutInflater inflater, ViewGroup container,
     38       Bundle savedInstanceState) {
     39     if (mapLayout == null) {
     40       Log.i("sys", "MF onCreateView() null");
     41       mapLayout = inflater.inflate(R.layout.map, null);
     42       mapView = (MapView) mapLayout.findViewById(R.id.mapView);
     43       mapView.onCreate(savedInstanceState);
     44       if (aMap == null) {
     45         aMap = mapView.getMap();
     46       }
     47     }else {
     48       if (mapLayout.getParent() != null) {
     49         ((ViewGroup) mapLayout.getParent()).removeView(mapLayout);
     50       }
     51     }
     52     return mapLayout;
     53   }
     54 
     55   @Override
     56   public void onAttach(Activity activity) {
     57     super.onAttach(activity);
     58     ((MainActivity) activity).onSectionAttached(Constants.MAP_FRAGMENT);
     59   }
     60   @Override
     61   public void onCreate(Bundle savedInstanceState) {
     62     super.onCreate(savedInstanceState);
     63     
     64   }
     65 
     66   @Override
     67   public void onResume() {
     68     Log.i("sys", "mf onResume");
     69     super.onResume();
     70     mapView.onResume();
     71   }
     72 
     73   /**
     74    * 方法必须重写
     75    * map的生命周期方法
     76    */
     77   @Override
     78   public void onPause() {
     79     Log.i("sys", "mf onPause");
     80     super.onPause();
     81     mapView.onPause();
     82   }
     83 
     84   /**
     85    * 方法必须重写
     86    * map的生命周期方法
     87    */
     88   @Override
     89   public void onSaveInstanceState(Bundle outState) {
     90     Log.i("sys", "mf onSaveInstanceState");
     91     super.onSaveInstanceState(outState);
     92     mapView.onSaveInstanceState(outState);
     93   }
     94 
     95   /**
     96    * 方法必须重写
     97    * map的生命周期方法
     98    */
     99   @Override
    100   public void onDestroy() {
    101     Log.i("sys", "mf onDestroy");
    102     super.onDestroy();
    103     mapView.onDestroy();
    104   }
    105 }

    这样可以保证在切换fragment的时候 地图不会不显示或者还原

    另外注意要在清单中注明app的方向 不能让屏幕翻转

    在Activity标签中写:

    1 android:screenOrientation="portrait"

    参考原文:http://www.tuicool.com/articles/FnqYRf

  • 相关阅读:
    通过结构体某个成员的地址计算结构体首地址 (转)
    2019.9.2课程设计报告
    2019.9.1课程设计报告
    2019.8.31课程设计报告
    2019.8.30课程设计报告
    2019.8.29课程设计报告
    2019.8.28课程设计报告
    2019.8.27课程设计报告
    2019.8.26课程设计报告
    2019.8.25课程设计报告
  • 原文地址:https://www.cnblogs.com/tanghuian/p/4048822.html
Copyright © 2011-2022 走看看