This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...mo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/IsochoneActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
MapboxAndroidDemo/src/main/res/layout/activity_isochrone.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:mapbox="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".examples.javaservices.IsochoneActivity"> | ||
|
||
<com.mapbox.mapboxsdk.maps.MapView | ||
android:id="@+id/mapView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
mapbox:mapbox_cameraTargetLat="37.7749" | ||
mapbox:mapbox_cameraTargetLng="-122.4194" | ||
mapbox:mapbox_cameraZoom="11" | ||
mapbox:mapbox_styleUrl="@string/mapbox_style_light" /> | ||
|
||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters