2014-11-03 58 views
1

我正在研究GoogleMaps,並且我在google地圖上放置了標記。標記信息我通過調用API獲取。如何在關閉應用程序時恢復用戶的以前的數據並顯示以前保存的實例?

現在我的問題是,如果用戶沒有互聯網連接,或者如果它關閉,我不希望該應用程序應該崩潰,而應該顯示用戶上次會話標記放置在谷歌地圖上。

我已經閱讀並嘗試了一些使用onPause()或onStart()方法可以完成的方法。但我沒有得到它:

源代碼看起來像這樣。

public class MapActivity extends Activity{ 
    // onCreate 
    // making an asynchronous call and storing the latitude and longitude of multiple marker in varibale place 
    doInBAckground(){ 
     place = apicall.getPlaces(); 
    } 
    onPostExecute(){ 
    plotmarkers(place); 
    } 

private void plotMarkers(List<Place> markers) { 
     if (markers.size() > 0) { 
      for (Place myMarker : markers) { 
       MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getLatitude(), myMarker.getLongitude())); 
       markerOption.icon(BitmapDescriptorFactory.fromResource(markerImage)); 

       Marker currentMarker = mMap.addMarker(markerOption); 
       mMarkersHashMap.put(currentMarker, myMarker); 
      } 
     } 
    } 
} 

現在我想要的是我應該能夠顯示用戶以前保存的實例。 我沒有得到這個工作..我見過OLA出租車應用程序能夠做到這一點。

請讓我知道如何處理它... 感謝名單提前.. 如果需要更多的片段只是讓我知道

回答

2

,你可以:

  • 讓你的HTTP客戶端緩存它適合你
  • 保存數據以及活動狀態
  • 堅持下載的數據到硬盤

你選擇哪一個去與取決於你多大的控制要在高速緩存中。

HTTP客戶端緩存 緩存很容易實現。您只需將您的HTTP客戶端配置爲使用磁盤緩存。使用OkHTTP那看起來會是這樣的:

private static final int DISK_CACHE_SIZE = 2 << 20; // 2MB cache 

OkHttpClient createOkHttpClient(Application app) { 
    OkHttpClient client = new OkHttpClient(); 

    // Install an HTTP cache in the application cache directory. 
    try { 
     File cacheDir = new File(app.getCacheDir(), "http"); 
     Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); 
     client.setCache(cache); 
    } catch (IOException e) { 
     LOG.e("<tag>", "Unable to install disk cache.", e); 
    } 
    return client; 
} 

與活動狀態保存 我不推薦這種方法。它只適用於您的在線數據變化如此之快以至於長時間持續不會對用戶有幫助的情況。在這種情況下,您可以將它與活動狀態一起保存,因爲它在那裏只持續很短的時間。閱讀關於實施這個here

堅持硬盤 這是我的首選解決方案。您可以以任何您喜歡的方式保存數據,然後再次加載。你可以使用共享首選項,或使用SQLite。如果您選擇使用SQLite,則應該查找使實現更容易的庫。我知道CupboardOrmLite都不錯。

+0

確定我將與SharedPreference方法嘗試...... – anand 2014-11-03 08:49:40

+0

感謝我得到了我與Sharedpefernce解決方案... – anand 2014-11-03 10:03:58

0

您可以覆蓋onSaveInstanceState()方法。

這是你如何保存你的數據:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    // save marker data 
    savedInstanceState.putString("coordinates", mMarker.getCoordinates()); 

    // always call the superclass so it can save the view hierarchy state 
    super.onSaveInstanceState(savedInstanceState); 
} 

注:後來,我建議實施在 您的機型(如地點,標記,的MarkerOptions)Parcelable接口,讓您輕鬆 放你的整個物體在捆綁中。它比 處理單個成員併爲它們的每個 創建單獨的密鑰更方便。

這是你如何找回你的onCreate()方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // check whether we're recreating a previously destroyed instance 
    if (savedInstanceState != null) { 
     // restore value from saved state 
     String coordinates = savedInstanceState.getString("coordinates"); 
     // do something with the restored data 
    } else { 
     // download from API 
     // do something with the downloaded data 
    } 
    ... 
} 

欲瞭解更多信息,請查看the official documentation

+0

我試過這種方法,但它總是給我onSaveInstance爲空.. – anand 2014-11-03 08:57:00

0

而其他的答案提供了一個很好的方式來保存標記,你也應該牢記在Google Maps terms of use列出的規則:

內容」指通過服務提供的任何內容(無論 通過創建谷歌或其第三方許可人),包括地圖數據,攝影圖像,交通數據,地點數據 (包括商業列表)或任何其他內容。

你不能預先擷取高速緩存,或儲存任何內容,除非你 可以存儲:(一)數量有限的內容以提高 如果你做你的地圖API實現的性能的目的所以 暫時(且在沒有事件超過30個歷日內) 牢固,並且在不允許使用內容 服務以外的方式;

相關問題