diff --git a/MapboxAndroidDemo/src/main/AndroidManifest.xml b/MapboxAndroidDemo/src/main/AndroidManifest.xml index 50c1e50c8..2ddaae347 100644 --- a/MapboxAndroidDemo/src/main/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/main/AndroidManifest.xml @@ -300,6 +300,13 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapbox.mapboxandroiddemo.MainActivity" /> + + + diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java index e42d2be6e..c1f7564b5 100644 --- a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java @@ -63,6 +63,7 @@ import com.mapbox.mapboxandroiddemo.examples.extrusions.RotationExtrusionActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.DirectionsActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.GeocodingActivity; +import com.mapbox.mapboxandroiddemo.examples.javaservices.IsochoneActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.MapMatchingActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.MatrixApiActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.OptimizationActivity; @@ -857,6 +858,14 @@ private void initializeModels() { null, R.string.activity_java_services_geocoding_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( + R.id.nav_java_services, + R.string.activity_java_services_isochrone_title, + R.string.activity_java_services_isochrone_description, + new Intent(MainActivity.this, IsochoneActivity.class), + null, + R.string.activity_java_services_isochrone_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( R.id.nav_snapshot_image_generator, R.string.activity_image_generator_snapshot_notification_title, diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/IsochoneActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/IsochoneActivity.java new file mode 100644 index 000000000..64fabc188 --- /dev/null +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/IsochoneActivity.java @@ -0,0 +1,141 @@ +package com.mapbox.mapboxandroiddemo.examples.javaservices; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; + +import com.mapbox.mapboxandroiddemo.R; +import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.style.layers.FillLayer; +import com.mapbox.mapboxsdk.style.layers.LineLayer; +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; + +import java.net.URL; + +import static com.mapbox.mapboxsdk.style.expressions.Expression.eq; +import static com.mapbox.mapboxsdk.style.expressions.Expression.geometryType; +import static com.mapbox.mapboxsdk.style.expressions.Expression.get; +import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOpacity; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; + +/** + * See how far a car can travel in certain time periods by requesting information from the Mapbox + * Isochrone API (https://www.mapbox.com/api-documentation/#isochrone) + */ +public class IsochoneActivity extends AppCompatActivity { + + private MapView mapView; + private MapboxMap mapboxMap; + private static final String GEOJSON_SOURCE_ID = "GEOJSON_SOURCE_ID"; + + @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_isochrone); + + mapView = findViewById(R.id.mapView); + mapView.onCreate(savedInstanceState); + mapView.getMapAsync(new OnMapReadyCallback() { + @Override + public void onMapReady(MapboxMap mapboxMap) { + IsochoneActivity.this.mapboxMap = mapboxMap; + initIsochroneGeoJsonSource(); + initFillLayer(); + initLineLayer(); + } + }); + } + + private void initIsochroneGeoJsonSource() { + try { + // Retrieve GeoJSON information from the Mapbox Isochrone API + GeoJsonSource source = new GeoJsonSource(GEOJSON_SOURCE_ID, new URL("https://api.mapbox.com/isochrone/v1/" + + "mapbox/driving/" + "-122.43817518306757,37.762834146042294?access_token=" + + getString(R.string.access_token) + "&polygons=false&" + "contours_minutes=5,10,15&contours_colors" + + "=6706ce,04e813,4286f4")); + + // Add the GeoJsonSource to map + mapboxMap.addSource(source); + + } catch (Throwable throwable) { + Log.e("IsochoneActivity", "Couldn't add GeoJsonSource to map", throwable); + } + } + + private void initFillLayer() { + // Create and style a FillLayer based on information in the Isochrone API response + FillLayer isochoneFillLayer = new FillLayer("polygon", GEOJSON_SOURCE_ID); + isochoneFillLayer.setProperties( + fillColor(get("color")), + fillOpacity(get("fillOpacity"))); + isochoneFillLayer.setFilter(eq(geometryType(), literal("Polygon"))); + isochoneFillLayer.setFilter(eq(literal("$type"), literal("Polygon"))); + mapboxMap.addLayer(isochoneFillLayer); + } + + private void initLineLayer() { + // Create and style a LineLayer based on information in the Isochrone API response + LineLayer isochoneLineLayer = new LineLayer("points", GEOJSON_SOURCE_ID); + isochoneLineLayer.setProperties( + lineColor(get("color")), + lineWidth(5f), + lineOpacity(get("opacity"))); + isochoneLineLayer.setFilter(eq(literal("$type"), literal("LineString"))); + mapboxMap.addLayer(isochoneLineLayer); + } + + @Override + protected void onStart() { + super.onStart(); + mapView.onStart(); + } + + @Override + public void onResume() { + super.onResume(); + mapView.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + mapView.onPause(); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mapView.onLowMemory(); + } + + @Override + protected void onStop() { + super.onStop(); + mapView.onStop(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + mapView.onDestroy(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mapView.onSaveInstanceState(outState); + } +} \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_isochrone.xml b/MapboxAndroidDemo/src/main/res/layout/activity_isochrone.xml new file mode 100644 index 000000000..2d2830043 --- /dev/null +++ b/MapboxAndroidDemo/src/main/res/layout/activity_isochrone.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml index 7d161b13c..33988dd8a 100644 --- a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml @@ -73,6 +73,7 @@ Use Mapbox Java Services to build a url and download a static map. Use the Directions Matrix API to receive driving times between 2-25 different locations. Use the Geocoding API to receive information about specific coordinates. + Use the Isochrone API to receive information about how far you can travel within a given time. Use the traffic plugin to display live car congestion data on top of a map. Use the building plugin to easily display 3D building height Use the location component to easily show a user\'s location on the map diff --git a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml index a5be9f44c..e59b6fd8f 100644 --- a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml @@ -72,6 +72,7 @@ Download a static map Retrieve travel times between many points Make a geocode request + Make a isochrone request Display real-time traffic Display buildings in 3D Show a user\'s location diff --git a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml index eca51fca2..294fba70f 100644 --- a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml @@ -73,6 +73,7 @@ http://i.imgur.com/guEQtlP.jpg https://i.imgur.com/65RlfRZ.png https://i.imgur.com/9xl3EF8.png + https://i.imgur.com/FHkeL7m.png http://i.imgur.com/HRriOVR.png http://i.imgur.com/Vcu67UR.png https://i.imgur.com/77PVsni.png