directions activity sample
This commit is contained in:
committed by
Brad Leege
parent
101a439b1d
commit
9c43343187
@@ -81,6 +81,11 @@ dependencies {
|
||||
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta1'
|
||||
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta1'
|
||||
|
||||
// Directions SDK
|
||||
compile ('com.mapbox.mapboxsdk:mapbox-android-directions:1.0.0-SNAPSHOT@aar'){
|
||||
transitive=true
|
||||
}
|
||||
|
||||
// Testing dependencies
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile 'org.mockito:mockito-core:1.10.19'
|
||||
|
||||
@@ -39,8 +39,9 @@
|
||||
android:name=".BulkMarkerActivity"
|
||||
android:configChanges="orientation|keyboardHidden|screenSize"
|
||||
android:label="@string/activity_add_bulk_markers" />
|
||||
<activity android:name=".CameraActivity"
|
||||
android:label="@string/activity_camera"/>
|
||||
<activity
|
||||
android:name=".CameraActivity"
|
||||
android:label="@string/activity_camera" />
|
||||
<activity
|
||||
android:name=".TiltActivity"
|
||||
android:label="@string/activity_tilt" />
|
||||
@@ -62,11 +63,13 @@
|
||||
<activity
|
||||
android:name=".CoordinateChangeActivity"
|
||||
android:label="@string/activity_animate_coordinate_change" />
|
||||
<activity
|
||||
android:name=".DirectionsActivity"
|
||||
android:label="@string/activity_directions" />
|
||||
|
||||
<meta-data
|
||||
android:name="com.mapbox.AccessToken"
|
||||
android:value="" />
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
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.MenuItem;
|
||||
|
||||
import com.mapbox.directions.DirectionsCriteria;
|
||||
import com.mapbox.directions.MapboxDirections;
|
||||
import com.mapbox.directions.service.models.DirectionsResponse;
|
||||
import com.mapbox.directions.service.models.DirectionsRoute;
|
||||
import com.mapbox.directions.service.models.Waypoint;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.utils.ApiAccess;
|
||||
import com.mapbox.mapboxsdk.views.MapView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import retrofit.Callback;
|
||||
import retrofit.Response;
|
||||
import retrofit.Retrofit;
|
||||
|
||||
public class DirectionsActivity extends AppCompatActivity {
|
||||
|
||||
private final static String LOG_TAG = "DirectionsActivity";
|
||||
|
||||
private MapView mMapView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_directions);
|
||||
|
||||
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.mapView);
|
||||
mMapView.setAccessToken(ApiAccess.getToken(this));
|
||||
mMapView.onCreate(savedInstanceState);
|
||||
|
||||
// load routes async
|
||||
loadRoute();
|
||||
}
|
||||
|
||||
private void loadRoute() {
|
||||
// Dupont Circle (Washington, DC)
|
||||
Waypoint origin = new Waypoint(-77.04341, 38.90962);
|
||||
|
||||
// The White House (Washington, DC)
|
||||
Waypoint destination = new Waypoint(-77.0365, 38.8977);
|
||||
|
||||
// Set map at centroid
|
||||
LatLng centroid = new LatLng(
|
||||
(origin.getLatitude() + destination.getLatitude()) / 2,
|
||||
(origin.getLongitude() + destination.getLongitude()) / 2);
|
||||
mMapView.setCenterCoordinate(centroid);
|
||||
mMapView.setZoomLevel(14);
|
||||
|
||||
// Add origin and destination to the map
|
||||
mMapView.addMarker(new MarkerOptions()
|
||||
.position(new LatLng(origin.getLatitude(), origin.getLongitude()))
|
||||
.title("Origin")
|
||||
.snippet("Dupont Circle"));
|
||||
mMapView.addMarker(new MarkerOptions()
|
||||
.position(new LatLng(destination.getLatitude(), destination.getLongitude()))
|
||||
.title("Destination")
|
||||
.snippet("The White House"));
|
||||
|
||||
// Get route from API
|
||||
getRoute(origin, destination);
|
||||
}
|
||||
|
||||
private void getRoute(Waypoint origin, Waypoint destination) {
|
||||
MapboxDirections md = new MapboxDirections.Builder()
|
||||
.setAccessToken(ApiAccess.getToken(this))
|
||||
.setOrigin(origin)
|
||||
.setDestination(destination)
|
||||
.setProfile(DirectionsCriteria.PROFILE_WALKING)
|
||||
.build();
|
||||
|
||||
md.enqueue(new Callback<DirectionsResponse>() {
|
||||
@Override
|
||||
public void onResponse(Response<DirectionsResponse> response, Retrofit retrofit) {
|
||||
// You can get generic HTTP info about the response
|
||||
Log.d(LOG_TAG, "Response code: " + response.code());
|
||||
|
||||
// Print some info about the route
|
||||
DirectionsRoute currentRoute = response.body().getRoutes().get(0);
|
||||
Log.d(LOG_TAG, "Distance: " + currentRoute.getDistance());
|
||||
|
||||
// Draw the route on the map
|
||||
drawRoute(currentRoute);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable t) {
|
||||
Log.e(LOG_TAG, "Error: " + t.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void drawRoute(DirectionsRoute route) {
|
||||
// Convert List<Waypoint> into LatLng[]
|
||||
List<Waypoint> waypoints = route.getGeometry().getWaypoints();
|
||||
LatLng[] point = new LatLng[waypoints.size()];
|
||||
for (int i = 0; i < waypoints.size(); i++) {
|
||||
point[i] = new LatLng(
|
||||
waypoints.get(i).getLatitude(),
|
||||
waypoints.get(i).getLongitude());
|
||||
}
|
||||
|
||||
// Draw Points on MapView
|
||||
mMapView.addPolyline(new PolylineOptions()
|
||||
.add(point)
|
||||
.color(Color.parseColor("#3887be"))
|
||||
.width(5));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -405,6 +405,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
return true;
|
||||
|
||||
case R.id.action_directions:
|
||||
startActivity(new Intent(getApplicationContext(), DirectionsActivity.class));
|
||||
return true;
|
||||
|
||||
default:
|
||||
return changeMapStyle(menuItem.getItemId());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportHeight="24.0"
|
||||
android:viewportWidth="24.0">
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:pathData="M21.71,11.29l-9,-9c-0.39,-0.39 -1.02,-0.39 -1.41,0l-9,9c-0.39,0.39 -0.39,1.02 0,1.41l9,9c0.39,0.39 1.02,0.39 1.41,0l9,-9c0.39,-0.38 0.39,-1.01 0,-1.41zM14,14.5V12h-4v3H8v-4c0,-0.55 0.45,-1 1,-1h5V7.5l3.5,3.5 -3.5,3.5z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportHeight="24.0"
|
||||
android:viewportWidth="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M12,2C8.13,2 5,5.13 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.87 -3.13,-7 -7,-7zm0,9.5c-1.38,0 -2.5,-1.12 -2.5,-2.5s1.12,-2.5 2.5,-2.5 2.5,1.12 2.5,2.5 -1.12,2.5 -2.5,2.5z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
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" />
|
||||
|
||||
<com.mapbox.mapboxsdk.views.MapView
|
||||
android:id="@+id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:style_url="@string/style_mapbox_streets" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -149,6 +149,12 @@
|
||||
android:icon="@drawable/ic_trending_up_24dp"
|
||||
android:title="@string/activity_polyline" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_directions"
|
||||
android:checkable="false"
|
||||
android:icon="@drawable/ic_directions"
|
||||
android:title="@string/activity_directions" />
|
||||
|
||||
</menu>
|
||||
</item>
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
<string name="activity_polyline">Polyline Activity</string>
|
||||
<string name="activity_animate_coordinate_change">Animate Coordinate Change</string>
|
||||
<string name="activity_custom_layer">Custom Layer</string>
|
||||
<string name="activity_directions">Directions Activity</string>
|
||||
|
||||
<string name="navdrawer_menu_title_mainactivity_controls">Main Activity Controls</string>
|
||||
<string name="navdrawer_menu_title_mainactivity_styles">Main Activity Styles</string>
|
||||
|
||||
@@ -17,6 +17,8 @@ buildscript {
|
||||
allprojects {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user