【Android筆記】Fragment中顯示高德地圖

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   io   os   ar   

本文來自: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   @Override100   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

【Android筆記】Fragment中顯示高德地圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.