Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.FloatRange;
Expand Down Expand Up @@ -2441,6 +2442,28 @@ void setMapboxMap(MapboxMap mapboxMap) {
mMapboxMap = mapboxMap;
}

@UiThread
void snapshot(@NonNull final MapboxMap.SnapshotReadyCallback callback, @Nullable final Bitmap bitmap) {
final TextureView textureView = (TextureView) findViewById(R.id.textureView);
final boolean canUseBitmap = bitmap != null && textureView.getWidth() == bitmap.getWidth() && textureView.getHeight() == bitmap.getHeight();

new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
if (canUseBitmap) {
return textureView.getBitmap(bitmap);
} else {
return textureView.getBitmap();
}
}

@Override
protected void onPostExecute(Bitmap bitmap) {
callback.onSnapshotReady(bitmap);
}
}.execute();
}

//
// View utility methods
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.mapboxsdk.maps;

import android.graphics.Bitmap;
import android.location.Location;
import android.os.SystemClock;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -1363,6 +1364,16 @@ public void invalidate() {
mMapView.update();
}

@UiThread
public void snapshot(@NonNull SnapshotReadyCallback callback, @Nullable final Bitmap bitmap) {
mMapView.snapshot(callback, bitmap);
}

@UiThread
public void snapshot(@NonNull SnapshotReadyCallback callback) {
mMapView.snapshot(callback, null);
}

//
// Interfaces
//
Expand Down Expand Up @@ -1584,6 +1595,10 @@ public interface CancelableCallback {
void onFinish();
}

public interface SnapshotReadyCallback {
void onSnapshotReady(Bitmap snapshot);
}

private class MapChangeCameraPositionListener implements MapView.OnMapChangedListener {

private static final long UPDATE_RATE_MS = 400;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@
android:name="@string/category"
android:value="@string/category_marker" />
</activity>
<activity
android:name=".activity.SnapshotActivity"
android:description="@string/description_snapshot"
android:label="@string/activity_snapshot">
<meta-data
android:name="@string/category"
android:value="@string/category_other" />
</activity>

<!-- Configuration Settings -->
<meta-data
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.mapbox.mapboxsdk.testapp.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ApiAccess;

public class SnapshotActivity extends AppCompatActivity {

private static final String TAG = "SnapshotActivity";

private MapView mMapView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_snapshot);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}

mMapView = (MapView) findViewById(R.id.cameraMapView);
mMapView.setAccessToken(ApiAccess.getToken(this));
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
// set a style
mapboxMap.setOnCameraChangeListener(new MapboxMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
Log.v(MapboxConstants.TAG, position.toString());
}
});

findViewById(R.id.makeSnapshotButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final long startTime = System.nanoTime();
mapboxMap.snapshot(new MapboxMap.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
long endTime = System.nanoTime();
long duration = (long) ((endTime - startTime) / 1e6);
ImageView snapshotView = (ImageView) findViewById(R.id.snapshotImageView);
snapshotView.setImageBitmap(snapshot);
Toast.makeText(SnapshotActivity.this, String.format("Snapshot taken in %d ms", duration), Toast.LENGTH_LONG).show();
}
});
}
});
}
});
}

@Override
protected void onStart() {
super.onStart();
mMapView.onStart();
}

@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}

@Override
protected void onStop() {
super.onStop();
mMapView.onStop();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}

@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:orientation="vertical" >
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/cameraMapView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/snapshotImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_make_snapshot"
android:id="@+id/makeSnapshotButton"
android:layout_margin="@dimen/fab_margin"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="@color/white"/>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="activity_offline">Offline Map Activity</string>
<string name="activity_minmax_zoom">Min/Max Zoom Activity</string>
<string name="activity_animated_marker">Animated Marker Activity</string>
<string name="activity_snapshot">Snapshot Activity</string>

<string name="description_user_location_tracking">Tracks the location of the user</string>
<string name="description_custom_layer">Add a custom layer, experimental feature</string>
Expand All @@ -56,6 +57,7 @@
<string name="description_directions">Example with Directions API</string>
<string name="description_geocoder">Example with Geocoder API</string>
<string name="description_scroll_by">Example to scroll with pixels in x,y direction</string>
<string name="description_snapshot">Example to make a snapshot of the map</string>

<string name="category_marker">Marker</string>
<string name="category_infowindow">InfoWindow</string>
Expand All @@ -70,6 +72,7 @@
<string name="button_camera_move">Move</string>
<string name="button_camera_ease">Ease</string>
<string name="button_camera_animate">Animate</string>
<string name="button_make_snapshot">Snapshot</string>

<string name="label_fps">FPS:</string>

Expand Down