1

I am working on GoogleMaps and and i placed the markers on the google map. The markers info i am getting by calling an api.

Now my problem is if a user is not having the Internet connectivity or if it is down i don't want that app should get crashed instead it should show the user last session markers placed on the google map.

I have read and tried some approaches like it can be done using onPause() or onStart() methods. But i am not getting it:

The source code looks something like this.

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);
            }
        }
    } 
}

Now what i want is I should be able to show the user previous saved instance. I am not getting this to work.. I have seen OLA cabs app able to do like this.

Please just let me know how to approach it... Thanx in advance.. If needed any more snippets just let me know

anand
  • 1,711
  • 2
  • 24
  • 59

3 Answers3

2

You could:

  • let your http client cache it for you
  • save the data along with the activity state
  • persist the downloaded data to the hard disk

Which one you choose to go with depends on how much control you want over the cache.

HTTP client cache Caching is easy to implement. You just configure your HTTP client to make use of a disk cache. Using OkHTTP that would look something this:

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;
}

Saving with activity state I do not recommend this approach. It's only suitable if your online data changes so quickly that persisting it for a long time is not helpful to the user. In that case you would save it with the activity state since there it is persisted for only a short time. Read about implementing this here.

Persisting to the hard disk This is my preferred solution. You can save the data in any way you like and load it again later. You could use Shared Preferences for that, or use SQLite. If you choose to go with SQLite you should look for libraries that make the implementation easier for you. I know Cupboard and OrmLite are pretty good.

Community
  • 1
  • 1
Jozua
  • 1,274
  • 10
  • 18
0

You can override the onSaveInstanceState() method.

This is how you save your data:

@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);
}

Note: Later, I recommend implementing Parcelable interface in your models (such as Place, Marker, MarkerOptions), so you can easily put your whole objects inside the Bundle. It's more convenient than dealing with single class members and creating a separate key for each of them.

And this is how you retrieve it on your onCreate() method:

@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
    }
    ...
}

For more information check the official documentation

EyesClear
  • 28,077
  • 7
  • 32
  • 43
0

While other answers provide a nice way to save the markers you should also keep in mind the rules listed in Google Maps terms of use:

"Content" means any content provided through the Service (whether created by Google or its third party licensors), including map and terrain data, photographic imagery, traffic data, places data (including business listings), or any other content.

You must not pre-fetch, cache, or store any Content, except that you may store: (i) limited amounts of Content for the purpose of improving the performance of your Maps API Implementation if you do so temporarily (and in no event for more than 30 calendar days), securely, and in a manner that does not permit use of the Content outside of the Service;

Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116