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

Commit

Permalink
added example
Browse files Browse the repository at this point in the history
  • Loading branch information
langsmith committed Nov 6, 2018
1 parent d35be85 commit 4c713b1
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 0 deletions.
8 changes: 8 additions & 0 deletions MapboxAndroidDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<uses-feature
Expand Down Expand Up @@ -370,6 +371,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.styles.ClickToAddImageActivity"
android:label="@string/activity_styles_click_to_add_image_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.query.QueryFeatureActivity"
android:label="@string/activity_query_feature_title">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
package com.mapbox.mapboxandroiddemo.examples.styles;

// #-code-snippet: click-to-add-image-activity full-java

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngQuad;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.style.sources.ImageSource;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;

/**
* Tap the map in four locations to set the bounds for an image that is selected from the device's gallery
* and then added to the map.
*/
public class ClickToAddImageActivity extends AppCompatActivity implements
OnMapReadyCallback, MapboxMap.OnMapClickListener {

private static final String ID_IMAGE_SOURCE = "source-id";
private static final String CIRCLE_SOURCE_ID = "circle-source-id";
private static final String ID_IMAGE_LAYER = "layer-id";
private MapView mapView;
private MapboxMap mapboxMap;
private LatLngQuad quad;
private List<Feature> boundsFeatureList;
private List<Point> boundsCirclePointList;
private static int PHOTO_PICK_CODE = 4;
private int imageCountIndex;

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

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
boundsFeatureList = new ArrayList<>();
boundsCirclePointList = new ArrayList<>();
this.mapboxMap = mapboxMap;
mapboxMap.addOnMapClickListener(this);
imageCountIndex = 0;
initCircleSource();
initCircleLayer();
Toast.makeText(this, R.string.tap_instructions, Toast.LENGTH_LONG).show();
}

@Override
public void onMapClick(@NonNull LatLng point) {

// Reset the lists once enough LatLngQuad points have been tapped
if (boundsFeatureList.size() == 4) {
boundsFeatureList = new ArrayList<>();
boundsCirclePointList = new ArrayList<>();
}

boundsFeatureList.add(Feature.fromGeometry(Point.fromLngLat(point.getLongitude(), point.getLatitude())));

// Add the click point to the circle layer and update the display of the circle layer data
boundsCirclePointList.add(Point.fromLngLat(point.getLongitude(), point.getLatitude()));
GeoJsonSource circleSource = mapboxMap.getSourceAs(CIRCLE_SOURCE_ID);
if (circleSource != null) {
circleSource.setGeoJson(FeatureCollection.fromFeatures(boundsFeatureList));
}

// Add to latLngList to eventually add a Polygon
List<LatLng> latLngList = new ArrayList<>();
for (Point singlePoint : boundsCirclePointList) {
latLngList.add(new LatLng(singlePoint.latitude(), singlePoint.longitude()));
}

// Once the 4 LatLngQuad points have been set for where the image will placed...
if (boundsCirclePointList.size() == 4) {

// Add polygon
mapboxMap.addPolygon(new PolygonOptions()
.addAll(latLngList)
.alpha(.3f)
.fillColor(Color.parseColor("#d004d3")));

// Create the LatLng objects to use in the LatLngQuad
LatLng latLng1 = new LatLng(boundsCirclePointList.get(0).latitude(),
boundsCirclePointList.get(0).longitude());
LatLng latLng2 = new LatLng(boundsCirclePointList.get(1).latitude(),
boundsCirclePointList.get(1).longitude());
LatLng latLng3 = new LatLng(boundsCirclePointList.get(2).latitude(),
boundsCirclePointList.get(2).longitude());
LatLng latLng4 = new LatLng(boundsCirclePointList.get(3).latitude(),
boundsCirclePointList.get(3).longitude());
quad = new LatLngQuad(latLng1, latLng2, latLng3, latLng4);

// Launch the intent to open the device's image gallery picker
Intent pickPhotoIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickPhotoIntent.setType("image/*");
startActivityForResult(pickPhotoIntent, PHOTO_PICK_CODE);
}
}

/**
* Set up the CircleLayer source for showing LatLngQuad map click points
*/
private void initCircleSource() {
FeatureCollection circleFeatureCollection = FeatureCollection.fromFeatures(new Feature[] {});
GeoJsonSource circleGeoJsonSource = new GeoJsonSource(CIRCLE_SOURCE_ID, circleFeatureCollection);
mapboxMap.addSource(circleGeoJsonSource);
}

/**
* Set up the CircleLayer for showing LatLngQuad map click points
*/
private void initCircleLayer() {
CircleLayer circleLayer = new CircleLayer("circle-layer-bounds-corner-id",
CIRCLE_SOURCE_ID);
circleLayer.setProperties(
circleRadius(8f),
circleColor(Color.parseColor("#d004d3"))
);
mapboxMap.addLayer(circleLayer);
}

/**
* Calling onActivityResult() to handle the return to the example from the device's image galleyr picker
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_PICK_CODE && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
Log.d("ClickToAddImageActivity", "data == null");
return;
}
Uri selectedImage = data.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);

Bitmap bitmapOfSelectedImage = BitmapFactory.decodeStream(imageStream);

// Create an ImageSource object
ImageSource imageSource = new ImageSource(ID_IMAGE_SOURCE + imageCountIndex, quad, bitmapOfSelectedImage);

// Add the imageSource to the map
mapboxMap.addSource(imageSource);

// Create a raster layer and use the imageSource's ID as the layer's data
RasterLayer layer = new RasterLayer(ID_IMAGE_LAYER + imageCountIndex,
ID_IMAGE_SOURCE + imageCountIndex);

// Add the layer to the map
mapboxMap.addLayer(layer);

// Reset lists in preparation for adding more images
boundsFeatureList = new ArrayList<>();
boundsCirclePointList = new ArrayList<>();

imageCountIndex++;

// Clear circles from CircleLayer
GeoJsonSource circleSource = mapboxMap.getSourceAs(CIRCLE_SOURCE_ID);
if (circleSource != null) {
circleSource.setGeoJson(FeatureCollection.fromFeatures(boundsFeatureList));
}

mapboxMap.clear();

} catch (FileNotFoundException exception) {
exception.printStackTrace();
}
}
}

// Add the mapView lifecycle to the activity's lifecycle methods
@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();
}

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

@Override
protected void onDestroy() {
super.onDestroy();
if (mapboxMap != null) {
mapboxMap.removeOnMapClickListener(this);
}
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
// #-end-code-snippet: click-to-add-image-activity full-java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="25.7836"
mapbox:mapbox_cameraTargetLng="-80.11725"
mapbox:mapbox_cameraZoom="5"
mapbox:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/>

</LinearLayout>
4 changes: 4 additions & 0 deletions MapboxAndroidDemo/src/main/res/values/activity_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,8 @@

<!-- Temperature change -->
<string name="temp_change_feedback"> Showing min and max temperatures up to 2006 in %1$s</string>

<!-- Click to add image -->
<string name="file_not_found">File not found</string>
<string name="tap_instructions">Tap four points on the map in a CLOCKWISE order</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="activity_style_hillshade_description">Use elevation data to show and customize hills and mountains.</string>
<string name="activity_styles_fade_switch_description">Fade map styles in and out based on zoom level.</string>
<string name="activity_styles_transparent_background_description">Create a transparent background and fill it with something such as moving water.</string>
<string name="activity_styles_click_to_add_image_description">Select a photo on the device and add it on the map tap location.</string>
<string name="activity_extrusions_catalina_marathon_extrusions_description">Use data-driven styling and GeoJSON data to set extrusions\' heights.</string>
<string name="activity_extrusions_population_density_extrusions_description">Use extrusions to display 3D building height based on imported vector data.</string>
<string name="activity_extrusions_adjust_extrusions_description">Change the location and color of the light shined on extrusions.</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/titles_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<string name="activity_styles_hillshade_title">Hillshading</string>
<string name="activity_styles_fade_switch_title">Switch map styles with fade</string>
<string name="activity_styles_transparent_background_title">Transparent render surface</string>
<string name="activity_styles_click_to_add_image_title">Click to add photo</string>
<string name="activity_extrusions_catalina_marathon_extrusions_title">Use GeoJSON data to set extrusion height</string>
<string name="activity_extrusions_population_density_extrusions_title">Display 3D building height based on vector data</string>
<string name="activity_extrusions_adjust_extrusions_title">Adjust light location and color</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 @@ -30,6 +30,7 @@
<string name="activity_style_hillshade_url" translatable="false">https://i.imgur.com/U2OKixV.png</string>
<string name="activity_styles_fade_switch_url" translatable="false">https://i.imgur.com/1sPnDx5.png</string>
<string name="activity_styles_transparent_background_url" translatable="false">https://i.imgur.com/5bYnlp5.png</string>
<string name="activity_styles_click_to_add_image_url" translatable="false">https://i.imgur.com/uPIH5Ck.png</string>
<string name="activity_extrusions_population_density_extrusions_url" translatable="false">http://i.imgur.com/se1z8Wb.png</string>
<string name="activity_extrusions_catalina_marathon_extrusions_url" translatable="false">http://i.imgur.com/6j848JL.jpg</string>
<string name="activity_extrusions_adjust_extrusions_url" translatable="false">http://i.imgur.com/XNTyIO5.png</string>
Expand Down

0 comments on commit 4c713b1

Please sign in to comment.