Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mapbox/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ dependencies {
// The Test App depends in Mapbox Android UI, which includes all modules
compile project(':libandroid-ui')

// Rx extensions
compile project(':libjava-services-rx')

// Android Support libraries
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mapbox.services.api.directions.v5.MapboxDirections;
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
import com.mapbox.services.api.directions.v5.models.DirectionsRoute;
import com.mapbox.services.api.rx.directions.v5.MapboxDirectionsRx;
import com.mapbox.services.commons.geojson.LineString;
import com.mapbox.services.commons.models.Position;

Expand All @@ -33,6 +34,9 @@
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class DirectionsV5Activity extends AppCompatActivity {

Expand Down Expand Up @@ -109,6 +113,25 @@ private void getRoute(Position origin, Position destination) throws ServicesExce
.setOverview(DirectionsCriteria.OVERVIEW_FULL)
.build();

MapboxDirectionsRx clientRx = new MapboxDirectionsRx.Builder()
.setAccessToken(Utils.getMapboxAccessToken(this))
.setCoordinates(positions)
.setProfile(DirectionsCriteria.PROFILE_DRIVING)
.setSteps(true)
.setOverview(DirectionsCriteria.OVERVIEW_FULL)
.build();
clientRx.getObservable()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<DirectionsResponse>() {
@Override
public void call(DirectionsResponse response) {
DirectionsRoute currentRoute = response.getRoutes().get(0);
Log.d(LOG_TAG, "Response code: " + response.getCode());
Log.d(LOG_TAG, "Distance: " + currentRoute.getDistance());
}
});

client.enqueueCall(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
Expand Down
1 change: 1 addition & 0 deletions mapbox/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ext {
gson: 'com.google.code.gson:gson:2.8.0',
retrofit2Main: 'com.squareup.retrofit2:retrofit:2.1.0',
retrofit2Gson: 'com.squareup.retrofit2:converter-gson:2.1.0',
retrofit2Rx: 'com.squareup.retrofit2:adapter-rxjava:2.1.0',
okhttp3Logging: 'com.squareup.okhttp3:logging-interceptor:3.5.0',
okhttp3Mockwebserver: 'com.squareup.okhttp3:mockwebserver:3.5.0',
junit: 'junit:junit:4.12',
Expand Down
8 changes: 8 additions & 0 deletions mapbox/libjava-services-rx/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ targetCompatibility = "1.7"
dependencies {
// Rx support depends on non-Rx services
compile project(':libjava-services')

// Rx extensions
compile rootProject.ext.dep.retrofit2Rx

// Testing
testCompile rootProject.ext.dep.okhttp3Mockwebserver
testCompile rootProject.ext.dep.junit
testCompile rootProject.ext.dep.hamcrestJunit
}

apply from: 'gradle-javadoc.gradle'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mapbox.services.api.rx.directions.v5;

import com.mapbox.services.api.directions.v5.models.DirectionsResponse;

import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Path;
import retrofit2.http.Query;
import rx.Observable;

/**
* Interface that defines the directions service (v5).
*
* @since 2.0.0
*/
public interface DirectionsServiceRx {

/**
* Observable-based interface
*
* @param userAgent The user.
* @param user The user.
* @param profile The profile directions should use.
* @param coordinates The coordinates the route should follow.
* @param accessToken Mapbox access token.
* @param alternatives Define whether you want to recieve more then one route.
* @param geometries Route geometry.
* @param overview Route full, simplified, etc.
* @param radiuses start at the most efficient point within the radius.
* @param steps Define if you'd like the route steps.
* @param continueStraight Define whether the route should continue straight even if the route
* will be slower.
* @return A retrofit Observable object
* @since 2.0.0
*/
@GET("directions/v5/{user}/{profile}/{coordinates}")
Observable<DirectionsResponse> getObservable(
@Header("User-Agent") String userAgent,
@Path("user") String user,
@Path("profile") String profile,
@Path("coordinates") String coordinates,
@Query("access_token") String accessToken,
@Query("alternatives") Boolean alternatives,
@Query("geometries") String geometries,
@Query("overview") String overview,
@Query("radiuses") String radiuses,
@Query("steps") Boolean steps,
@Query("continue_straight") Boolean continueStraight
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.mapbox.services.api.rx.directions.v5;

import com.mapbox.services.api.ServicesException;
import com.mapbox.services.api.directions.v5.MapboxDirections;
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
* The Directions API allows the calculation of routes between coordinates. The fastest route
* is returned with geometries, and turn-by-turn instructions. The Mapbox Directions API supports
* routing for driving cars, riding bicycles and walking.
*
* This class has support for Rx Observables.
*
* @since 2.0.0
*/
public class MapboxDirectionsRx extends MapboxDirections {

private DirectionsServiceRx serviceRx = null;
private Observable<DirectionsResponse> observable = null;

public MapboxDirectionsRx(Builder builder) {
super(builder);
}

private DirectionsServiceRx getServiceRx() {
// No need to recreate it
if (serviceRx != null) {
return serviceRx;
}

// Retrofit instance
Retrofit retrofit = new Retrofit.Builder()
.client(getOkHttpClient())
.baseUrl(builder.getBaseUrl())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();

// Directions service
serviceRx = retrofit.create(DirectionsServiceRx.class);
return serviceRx;
}

public Observable<DirectionsResponse> getObservable() {
// No need to recreate it
if (observable != null) {
return observable;
}

observable = getServiceRx().getObservable(
getHeaderUserAgent(builder.getClientAppName()),
builder.getUser(),
builder.getProfile(),
builder.getCoordinates(),
builder.getAccessToken(),
builder.isAlternatives(),
builder.getGeometries(),
builder.getOverview(),
builder.getRadiuses(),
builder.isSteps(),
builder.isContinueStraight());

// Done
return observable;
}

public static class Builder extends MapboxDirections.Builder<Builder> {
@Override
public MapboxDirectionsRx build() throws ServicesException {
super.build();
return new MapboxDirectionsRx(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mapbox.services.api.rx.distance.v1;

import com.mapbox.services.api.distance.v1.models.DistanceResponse;
import com.mapbox.services.commons.geojson.MultiPoint;

import okhttp3.RequestBody;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
import rx.Observable;

/**
* Interface that defines the distance service.
*
* @since 2.0.0
*/
public interface DistanceServiceRx {
/**
* Observable-based interface
*
* @param userAgent The User.
* @param user The user.
* @param profile Directions profile id.
* @param accessToken Mapbox access token.
* @param coordinates converted to a {@link MultiPoint#toJson()}.
* @return The {@link DistanceResponse} in a Observable wrapper
* @since 2.0.0
*/
@POST("distances/v1/{user}/{profile}")
Observable<DistanceResponse> getObservable(
@Header("User-Agent") String userAgent,
@Path("user") String user,
@Path("profile") String profile,
@Query("access_token") String accessToken,
@Body RequestBody coordinates);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.mapbox.services.api.rx.distance.v1;

import com.mapbox.services.api.ServicesException;
import com.mapbox.services.api.distance.v1.MapboxDistance;
import com.mapbox.services.api.distance.v1.models.DistanceResponse;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
* Note this API is still in preview.
* <p>
* The Mapbox Distance API returns all travel times between many points. For example, given 3
* locations A, B, C, the Distance API will return a matrix of travel times in seconds between each
* location. This API allows you to build tools that efficiently check the reachability of
* coordinates from each other, filter points by travel time, or run algorithms for solving
* optimization problems.
* </p>
* <p>
* Limits placed on this API include a maximum 100 coordinate pairs per request and a maximum 60
* requests per minute.
* </p>
* This class has support for Rx Observables.
*
* @see <a href="https://www.mapbox.com/api-documentation/?language=Java#distance">Mapbox Distance API documentation</a>
* @since 2.0.0
*/
public class MapboxDistanceRx extends MapboxDistance {

private DistanceServiceRx serviceRx = null;
private Observable<DistanceResponse> observable = null;

public MapboxDistanceRx(Builder builder) {
super(builder);
}

private DistanceServiceRx getServiceRx() {
// No need to recreate it
if (serviceRx != null) {
return serviceRx;
}

// Retrofit instance
Retrofit retrofit = new Retrofit.Builder()
.client(getOkHttpClient())
.baseUrl(builder.getBaseUrl())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(getGson()))
.build();

// Distance service
serviceRx = retrofit.create(DistanceServiceRx.class);
return serviceRx;
}

public Observable<DistanceResponse> getObservable() {
// No need to recreate it
if (observable != null) {
return observable;
}

observable = getServiceRx().getObservable(
getHeaderUserAgent(builder.getClientAppName()),
builder.getUser(),
builder.getProfile(),
builder.getAccessToken(),
builder.getCoordinates()
);

// Done
return observable;
}

public static class Builder extends MapboxDistance.Builder<Builder> {
@Override
public MapboxDistanceRx build() throws ServicesException {
super.build();
return new MapboxDistanceRx(this);
}
}

}
Loading