Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Snapshot share example (#555)
Browse files Browse the repository at this point in the history
* Added new  category, and created SnapshotListActivity, based on MapSnapshotterActivity in the TestApp

* Added comments

* Fixed crashes by removing added null, setting currentCategory and adding break

* Added activity to manifest and added access token

* Added SnapshotNotificationActivity which shows a notification with an image when the map is clicked on

* Added text view to bottom to replace toast to alert user of how to use demo

* Added PendingIntent so that clicking on notification re-opens app

* Refactored names and changed list to grid to more accurately describe contents

* Added screenshot image urls

* Reverted changes to wrong layout file and created new layout file for SnapshotNotificationActivity

* Re-added newline that I accidentally removed

* Cleaned up code and removed array of snapshotters and instead replaced with one

* Added more comments to SnapshotGridActivity

* Changed indentation

* Added new lines to ends of files and removed unnecessary line

* checkstyle indentation

* checkstyle line length

* checkstyle var name

* Added description box to offline maps so that users know that Snapshotter exists

* Updated banner copy

* Revert "Added description box to offline maps so that users know that Snapshotter exists"

This reverts commit c08615b.

* Added banner to top of list in layout for Downloading a static map

* Added on click listener to banner for static maps to link to Snapshotter demos

* consistency for end of sentence periodos

* comment added to new public constant

* edit comment for accuracy

* initial setup

* cleanup

* checkstyle

* removed snapshot grid stuff

* refactored ig name to snapshot

* added boolean check to block crash

* tweaked intent settings
  • Loading branch information
Langston Smith authored Dec 8, 2017
1 parent 16bc2b2 commit 7dda338
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 98 deletions.
180 changes: 93 additions & 87 deletions MapboxAndroidDemo/src/main/AndroidManifest.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
import com.mapbox.mapboxandroiddemo.examples.extrusions.MarathonExtrusionActivity;
import com.mapbox.mapboxandroiddemo.examples.extrusions.PopulationDensityExtrusionActivity;
import com.mapbox.mapboxandroiddemo.examples.extrusions.RotationExtrusionActivity;
import com.mapbox.mapboxandroiddemo.examples.ig.SnapshotNotificationActivity;
import com.mapbox.mapboxandroiddemo.examples.snapshot.SnapshotNotificationActivity;
import com.mapbox.mapboxandroiddemo.examples.snapshot.SnapshotShareActivity;
import com.mapbox.mapboxandroiddemo.examples.mas.DirectionsActivity;
import com.mapbox.mapboxandroiddemo.examples.mas.DirectionsMatrixApiActivity;
import com.mapbox.mapboxandroiddemo.examples.mas.GeocodingActivity;
Expand Down Expand Up @@ -155,9 +156,9 @@ protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
currentCategory = savedInstanceState.getInt("CURRENT_CATEGORY");
listItems(currentCategory);
} else if (getIntent().getIntExtra(EXTRA_NAV, -1) == R.id.nav_image_generator) {
currentCategory = R.id.nav_image_generator;
listItems(R.id.nav_image_generator);
} else if (getIntent().getIntExtra(EXTRA_NAV, -1) == R.id.nav_snapshot_image_generator) {
currentCategory = R.id.nav_snapshot_image_generator;
listItems(R.id.nav_snapshot_image_generator);
} else {
listItems(R.id.nav_basics);
}
Expand Down Expand Up @@ -575,14 +576,20 @@ private void listItems(int id) {
R.string.activity_mas_matrix_url));
currentCategory = R.id.nav_mas;
break;
case R.id.nav_image_generator:
case R.id.nav_snapshot_image_generator:
exampleItemModel.add(new ExampleItemModel(
R.string.activity_image_generator_snapshot_notification_title,
R.string.activity_image_generator_snapshot_notification_description,
new Intent(MainActivity.this, SnapshotNotificationActivity.class),
R.string.activity_image_generator_snapshot_notification_url
));
currentCategory = R.id.nav_image_generator;
exampleItemModel.add(new ExampleItemModel(
R.string.activity_image_generator_snapshot_share_title,
R.string.activity_image_generator_snapshot_share_description,
new Intent(MainActivity.this, SnapshotShareActivity.class),
R.string.activity_image_generator_snapshot_share_url
));
currentCategory = R.id.nav_snapshot_image_generator;
break;
case R.id.nav_lab:
exampleItemModel.add(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View view) {
Intent intent = new Intent(StaticImageActivity.this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_NAV, R.id.nav_image_generator);
intent.putExtra(MainActivity.EXTRA_NAV, R.id.nav_snapshot_image_generator);
startActivity(intent);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mapbox.mapboxandroiddemo.examples.ig;
package com.mapbox.mapboxandroiddemo.examples.snapshot;

import android.app.NotificationManager;
import android.app.PendingIntent;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package com.mapbox.mapboxandroiddemo.examples.snapshot;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class SnapshotShareActivity extends AppCompatActivity {
private MapView mapView;
private MapSnapshotter mapSnapshotter;
private MapboxMap mapboxMap;
private FloatingActionButton cameraFab;
private boolean hasStartedSnapshotGeneration;

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

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_snapshot_share);

cameraFab = findViewById(R.id.camera_share_snapshot_image_fab);
cameraFab.setImageResource(R.drawable.ic_camera);

hasStartedSnapshotGeneration = false;

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);

// Set a callback for when MapboxMap is ready to be used
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final MapboxMap mapboxMap) {

SnapshotShareActivity.this.mapboxMap = mapboxMap;

// When user clicks the map, start the snapshotting process with the given parameters
cameraFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (!hasStartedSnapshotGeneration) {
hasStartedSnapshotGeneration = true;
Toast.makeText(SnapshotShareActivity.this, R.string.loading_snapshot_image, Toast.LENGTH_LONG).show();
startSnapShot(
mapboxMap.getProjection().getVisibleRegion().latLngBounds,
mapView.getMeasuredHeight(),
mapView.getMeasuredWidth());
}
}
});
}
});
}

/**
* Creates bitmap from given parameters, and creates a notification with that bitmap
*
* @param latLngBounds of map
* @param height of map
* @param width of map
*/
private void startSnapShot(LatLngBounds latLngBounds, int height, int width) {
if (mapSnapshotter == null) {
// Initialize snapshotter with map dimensions and given bounds
MapSnapshotter.Options options =
new MapSnapshotter.Options(width, height).withRegion(latLngBounds).withStyle(mapboxMap.getStyleUrl());

mapSnapshotter = new MapSnapshotter(SnapshotShareActivity.this, options);
} else {
// Reuse pre-existing MapSnapshotter instance
mapSnapshotter.setSize(width, height);
mapSnapshotter.setRegion(latLngBounds);
mapSnapshotter.setRegion(latLngBounds);
}

mapSnapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(MapSnapshot snapshot) {

Bitmap bitmapOfMapSnapshotImage = snapshot.getBitmap();

Uri bmpUri = getLocalBitmapUri(bitmapOfMapSnapshotImage);

Intent shareIntent = new Intent();
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/png");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share map image"));

hasStartedSnapshotGeneration = false;
}
});
}

private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
try {
out.close();
} catch (IOException exception) {
exception.printStackTrace();
}
bmpUri = Uri.fromFile(file);
} catch (FileNotFoundException exception) {
exception.printStackTrace();
}
return bmpUri;
}

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

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

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

@Override
public void onPause() {
super.onPause();
mapView.onPause();
// Make sure to stop the snapshotter on pause if it exists
if (mapSnapshotter != null) {
mapSnapshotter.cancel();
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

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

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".examples.ig.SnapshotNotificationActivity">
tools:context=".examples.snapshot.SnapshotNotificationActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
Expand Down
26 changes: 26 additions & 0 deletions MapboxAndroidDemo/src/main/res/layout/activity_snapshot_share.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="37.536172"
mapbox:mapbox_cameraTargetLng="126.916954"
mapbox:mapbox_cameraTilt="60"
mapbox:mapbox_cameraZoom="9"
mapbox:mapbox_styleUrl="@string/mapbox_style_satellite_streets" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/camera_share_snapshot_image_fab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
fab:fab_size="normal" />

</android.support.design.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
android:title="@string/android_services_category"/>

<item
android:id="@+id/nav_image_generator"
android:id="@+id/nav_snapshot_image_generator"
android:title="@string/image_generator_category"/>

<item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<string name="activity_plugins_geojson_marker_clusters_description">Use the plugin to automatically add marker clusters to the map. This example shows bike share stations in Paris.</string>
<string name="activity_mas_simplify_polyline_description">Using the polylines utility, simplify a polyline which reduces the amount of coordinates making up the polyline depending on tolerance.</string>
<string name="activity_mas_map_matching_description">Match raw GPS points to the map so they aligns with the roads/pathways.</string>
<string name="activity_image_generator_snapshot_share_description">Send and share a map snapshot image.</string>
<string name="activity_image_generator_snapshot_notification_description">Show a snapshotted image in a notification.</string>
<string name="activity_lab_location_picker_description">Drop a marker at a specific location and then perform reverse geocoding.</string>
<string name="activity_lab_marker_following_route_description">Using a map matched GeoJSON route, the marker travels along the route at consistent speed.</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<string name="description_query">Examples here show how to query the map data to obtain information such as a features geometry or properties.</string>
<string name="description_offline">We have a new way to make client-side static maps. We call it the Map Snapshotter, click here for some examples.</string>

<!-- snapshotter -->
<string name="loading_snapshot_image">Loading snapshot image</string>

<!-- toolbar items -->
<string name="info">Info</string>

Expand Down
3 changes: 2 additions & 1 deletion MapboxAndroidDemo/src/main/res/values/titles_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
<string name="activity_plugins_marker_clusters_plugin_title">Add marker clusters</string>
<string name="activity_mas_simplify_polyline_title">Simplify a polyline</string>
<string name="activity_mas_map_matching_title">Map Matching</string>
<string name="activity_image_generator_snapshot_notification_title">Snapshot notification</string>
<string name="activity_image_generator_snapshot_notification_title">Snapshot Notification</string>
<string name="activity_image_generator_snapshot_share_title">Share snapshot image</string>
<string name="activity_lab_location_picker_title">Location picker</string>
<string name="activity_lab_rv_on_map_title">Recyclerview interaction</string>
<string name="activity_lab_rv_symbol_layer_on_map_title">Recyclerview + SymbolLayer markers</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/urls_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<string name="activity_mas_simplify_polyline_url" translatable="false">http://i.imgur.com/uATgul1.png</string>
<string name="activity_mas_map_matching_url" translatable="false">http://i.imgur.com/bWvVbwC.png</string>
<string name="activity_image_generator_snapshot_notification_url" translatable="false">https://i.imgur.com/OiveDFG.png</string>
<string name="activity_image_generator_snapshot_share_url" translatable="false">https://i.imgur.com/KgfsY3s.png</string>
<string name="activity_lab_location_picker_url" translatable="false">http://i.imgur.com/0JTVwaa.png</string>
<string name="activity_lab_marker_following_route_url" translatable="false">http://i.imgur.com/spsZu9X.png</string>
<string name="activity_lab_space_station_location_url" translatable="false">http://i.imgur.com/PxuB1T8.png</string>
Expand Down

0 comments on commit 7dda338

Please sign in to comment.