Skip to content

Instantly share code, notes, and snippets.

@tobrun
Created May 12, 2016 08:20
Show Gist options
  • Save tobrun/9f9f88f6bd150fc94e0eb2cf9c39e49d to your computer and use it in GitHub Desktop.
Save tobrun/9f9f88f6bd150fc94e0eb2cf9c39e49d to your computer and use it in GitHub Desktop.
Offline sample
<?xml version="1.0" encoding="utf-8"?>
<com.mapbox.mapboxsdk.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview"
android:layout_width="match_parent"
app:center_latitude="65.970773"
app:center_longitude="-18.532800"
app:zoom="16"
android:layout_height="match_parent" />
public class MainActivity extends AppCompatActivity {
private MapView mMapView;
private OfflineManager mOfflineManager;
private OfflineRegion mOfflineRegion;
private MapboxMap mapboxMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setAccessToken("pk.eyJ1IjoidG9icnVuIiwiYSI6ImNpajVlajR0cjAwNjN2NmtyY204eWw2eG0ifQ.x3_WEoExNW5Qyv9T3Vj7Mw");
mMapView.setStyleUrl(Style.DARK);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap map) {
mapboxMap = map;
Toast.makeText(MainActivity.this, "OnMapReady", Toast.LENGTH_LONG).show();
mOfflineManager = OfflineManager.getInstance(MainActivity.this);
mOfflineManager.setAccessToken("pk.eyJ1IjoidG9icnVuIiwiYSI6ImNpajVlajR0cjAwNjN2NmtyY204eWw2eG0ifQ.x3_WEoExNW5Qyv9T3Vj7Mw");
mOfflineManager.listOfflineRegions(new OfflineManager.ListOfflineRegionsCallback() {
@Override
public void onList(OfflineRegion[] offlineRegions) {
// Check result
if (offlineRegions == null || offlineRegions.length == 0) {
Log.e("TAG", "YOU HAVE NO REGIONS, START DOWNLOAD");
downloadRegion();
return;
}
Log.v("TAG", "WE HAVE A REGION!");
}
@Override
public void onError(String error) {
Log.e("TAG", "Error: " + error);
}
});
}
});
}
private void downloadRegion() {
Projection projection = mapboxMap.getProjection();
LatLng topLeft = projection.fromScreenLocation(new PointF(0, 0));
LatLng bottomRight = projection.fromScreenLocation(
new PointF(mMapView.getWidth(), mMapView.getHeight()));
LatLngBounds bounds = new LatLngBounds.Builder()
.include(topLeft)
.include(bottomRight)
.build();
Log.v("TAG", topLeft + " " + bottomRight);
Log.v("TAG", bounds.toString());
mOfflineManager.createOfflineRegion(new OfflineTilePyramidRegionDefinition(
Style.MAPBOX_STREETS,
bounds,
mapboxMap.getCameraPosition().zoom,
mapboxMap.getMaxZoom(),
1.0f
), new byte[0], new OfflineManager.CreateOfflineRegionCallback() {
@Override
public void onCreate(OfflineRegion offlineRegion) {
offlineRegion.setDownloadState(OfflineRegion.STATE_ACTIVE);
offlineRegion.setObserver(new OfflineRegion.OfflineRegionObserver() {
@Override
public void onStatusChanged(OfflineRegionStatus status) {
if (status.isComplete()) {
Toast.makeText(MainActivity.this, "Done saving map.", Toast.LENGTH_SHORT).show();
}
Log.d("TAG", "completed resource size " + status.getCompletedResourceSize());
}
@Override
public void onError(OfflineRegionError error) {
Log.e("TAG", "ERROR " + error);
}
@Override
public void mapboxTileCountLimitExceeded(long limit) {
Log.e("TAG", "LIMIT REACHED " + limit);
}
});
}
@Override
public void onError(String error) {
Log.e("TAG", "ERROR " + error);
}
});
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment