Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Maps SDK 7.x and events 4.x with new location APIs #1615

Merged
merged 1 commit into from
Jan 14, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.widget.Toast;

import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineCallback;
import com.mapbox.android.core.location.LocationEngineResult;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;
Expand All @@ -28,6 +30,7 @@
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.services.android.navigation.testapp.R;
import com.mapbox.services.android.navigation.testapp.Utils;
import com.mapbox.services.android.navigation.testapp.activity.notification.CustomNavigationNotification;
Expand Down Expand Up @@ -170,25 +173,24 @@ private void newOrigin() {

@SuppressLint("MissingPermission")
@Override
public void onMapReady(MapboxMap mapboxMap) {
public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;

LocationComponent locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(this);
locationComponent.setRenderMode(RenderMode.GPS);
locationComponent.setLocationComponentEnabled(false);
navigationMapRoute = new NavigationMapRoute(navigation, mapView, mapboxMap);

mapboxMap.addOnMapClickListener(this);
Snackbar.make(findViewById(R.id.container), "Tap map to place waypoint", BaseTransientBottomBar.LENGTH_LONG).show();

locationEngine = new ReplayRouteLocationEngine();

newOrigin();
this.mapboxMap.addOnMapClickListener(this);
mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> {
LocationComponent locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(this, style);
locationComponent.setRenderMode(RenderMode.GPS);
locationComponent.setLocationComponentEnabled(false);
navigationMapRoute = new NavigationMapRoute(navigation, mapView, mapboxMap);
Snackbar.make(findViewById(R.id.container), "Tap map to place waypoint",
BaseTransientBottomBar.LENGTH_LONG).show();
locationEngine = new ReplayRouteLocationEngine();
newOrigin();
});
}

@Override
public void onMapClick(@NonNull LatLng point) {
public boolean onMapClick(@NonNull LatLng point) {
if (destination == null) {
destination = Point.fromLngLat(point.getLongitude(), point.getLatitude());
} else if (waypoint == null) {
Expand All @@ -198,15 +200,30 @@ public void onMapClick(@NonNull LatLng point) {
}
mapboxMap.addMarker(new MarkerOptions().position(point));
calculateRoute();
return true;
}

@SuppressLint("MissingPermission")
private void calculateRoute() {
Location userLocation = locationEngine.getLastLocation();
locationEngine.getLastLocation(new LocationEngineCallback<LocationEngineResult>() {
@Override
public void onSuccess(LocationEngineResult result) {
findRouteWith(result);
}

@Override
public void onFailure(@NonNull Exception exception) {
Timber.e(exception);
}
});
}

private void findRouteWith(LocationEngineResult result) {
Location userLocation = result.getLastLocation();
if (userLocation == null) {
Timber.d("calculateRoute: User location is null, therefore, origin can't be set.");
return;
}

Point origin = Point.fromLngLat(userLocation.getLongitude(), userLocation.getLatitude());
if (TurfMeasurement.distance(origin, destination, TurfConstants.UNIT_METERS) < 50) {
startRouteButton.setVisibility(View.GONE);
Expand All @@ -223,20 +240,19 @@ private void calculateRoute() {

navigationRouteBuilder.build().getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
public void onResponse(@NonNull Call<DirectionsResponse> call, @NonNull Response<DirectionsResponse> response) {
Timber.d("Url: %s", call.request().url().toString());
if (response.body() != null) {
if (!response.body().routes().isEmpty()) {
DirectionsRoute directionsRoute = response.body().routes().get(0);
MockNavigationActivity.this.route = directionsRoute;
MockNavigationActivity.this.route = response.body().routes().get(0);
navigationMapRoute.addRoutes(response.body().routes());
startRouteButton.setVisibility(View.VISIBLE);
}
}
}

@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
public void onFailure(@NonNull Call<DirectionsResponse> call, @NonNull Throwable throwable) {
Timber.e(throwable, "onFailure: navigation.getRoute()");
}
});
Expand Down Expand Up @@ -310,8 +326,6 @@ public void onLowMemory() {
protected void onDestroy() {
super.onDestroy();
navigation.onDestroy();
locationEngine.removeLocationUpdates();
locationEngine.deactivate();
if (mapboxMap != null) {
mapboxMap.removeOnMapClickListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.widget.Toast
import com.mapbox.geojson.BoundingBox
import com.mapbox.mapboxsdk.Mapbox
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.layers.FillLayer
import com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
Expand Down Expand Up @@ -113,17 +114,13 @@ class OfflineRegionDownloadActivity : AppCompatActivity(), RouteTileDownloadList
private fun setupMapView(savedInstanceState: Bundle?) {
mapView.onCreate(savedInstanceState)
mapView.getMapAsync { mapboxMap ->
mapboxMap.setStyle(Style.LIGHT) {
it.addSource(GeoJsonSource("bounding-box-source"))
it.addLayer(FillLayer("bounding-box-layer", "bounding-box-source")
.withProperties(fillColor(Color.parseColor("#50667F"))))
}
this.mapboxMap = mapboxMap
mapboxMap.uiSettings.isRotateGesturesEnabled = false
addBoundingBoxToMap()
}
}

private fun addBoundingBoxToMap() {
mapboxMap.apply {
addSource(GeoJsonSource("bounding-box-source"))
addLayer(FillLayer("bounding-box-layer", "bounding-box-source")
.withProperties(fillColor(Color.parseColor("#50667F"))))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import android.view.View;
import android.widget.Toast;

import com.mapbox.android.core.location.LocationEngineListener;
import com.mapbox.android.core.location.LocationEngineCallback;
import com.mapbox.android.core.location.LocationEngineResult;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.core.constants.Constants;
Expand All @@ -27,6 +28,7 @@
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.services.android.navigation.testapp.R;
import com.mapbox.services.android.navigation.ui.v5.instruction.InstructionView;
import com.mapbox.services.android.navigation.v5.location.replay.ReplayRouteLocationEngine;
Expand All @@ -41,6 +43,7 @@
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -51,9 +54,9 @@
import retrofit2.Response;
import timber.log.Timber;

public class RerouteActivity extends HistoryActivity implements OnMapReadyCallback, LocationEngineListener,
Callback<DirectionsResponse>, MapboxMap.OnMapClickListener, NavigationEventListener, OffRouteListener,
ProgressChangeListener, MilestoneEventListener {
public class RerouteActivity extends HistoryActivity implements OnMapReadyCallback,
Callback<DirectionsResponse>, MapboxMap.OnMapClickListener, NavigationEventListener,
OffRouteListener, ProgressChangeListener, MilestoneEventListener {

@BindView(R.id.mapView)
MapView mapView;
Expand All @@ -66,6 +69,8 @@ public class RerouteActivity extends HistoryActivity implements OnMapReadyCallba
private Point destination = Point.fromLngLat(-0.383524, 39.497825);
private Polyline polyline;

private final RerouteActivityLocationCallback callback = new RerouteActivityLocationCallback(this);
private Location lastLocation;
private ReplayRouteLocationEngine mockLocationEngine;
private MapboxNavigation navigation;
private MapboxMap mapboxMap;
Expand Down Expand Up @@ -141,47 +146,34 @@ protected void onDestroy() {

@SuppressLint("MissingPermission")
@Override
public void onMapReady(MapboxMap mapboxMap) {
public void onMapReady(@NonNull MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.addOnMapClickListener(this);

LocationComponent locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(this);
locationComponent.setLocationComponentEnabled(true);
locationComponent.setRenderMode(RenderMode.GPS);

mockLocationEngine = new ReplayRouteLocationEngine();
mockLocationEngine.addLocationEngineListener(this);
navigation.setLocationEngine(mockLocationEngine);

getRoute(origin, destination, null);
this.mapboxMap.addOnMapClickListener(this);
mapboxMap.setStyle(Style.DARK, style -> {
LocationComponent locationComponent = mapboxMap.getLocationComponent();
locationComponent.activateLocationComponent(this, style);
locationComponent.setLocationComponentEnabled(true);
locationComponent.setRenderMode(RenderMode.GPS);

mockLocationEngine = new ReplayRouteLocationEngine();
getRoute(origin, destination);
});
}

@Override
public void onConnected() {
// No-op - mock automatically begins pushing updates
}

@Override
public void onLocationChanged(Location location) {
if (!tracking) {
mapboxMap.getLocationComponent().forceLocationUpdate(location);
}
}

@Override
public void onMapClick(@NonNull LatLng point) {
if (!running || mapboxMap == null) {
return;
public boolean onMapClick(@NonNull LatLng point) {
if (!running || mapboxMap == null || lastLocation == null) {
return true;
}

mapboxMap.addMarker(new MarkerOptions().position(point));
mapboxMap.removeOnMapClickListener(this);

Point newDestination = Point.fromLngLat(point.getLongitude(), point.getLatitude());
mockLocationEngine.moveTo(newDestination);
destination = Point.fromLngLat(point.getLongitude(), point.getLatitude());
resetLocationEngine(destination);

tracking = false;
return true;
}

@Override
Expand All @@ -195,15 +187,16 @@ public void onRunning(boolean running) {

@Override
public void userOffRoute(Location location) {
Point newOrigin = Point.fromLngLat(location.getLongitude(), location.getLatitude());
getRoute(newOrigin, destination, location.getBearing());
origin = Point.fromLngLat(lastLocation.getLongitude(), lastLocation.getLatitude());
getRoute(origin, destination);
Snackbar.make(contentLayout, "User Off Route", Snackbar.LENGTH_SHORT).show();
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())));
}

@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
boolean isInTunnel = routeProgress.inTunnel();
lastLocation = location;
if (!wasInTunnel && isInTunnel) {
wasInTunnel = true;
Snackbar.make(contentLayout, "Enter tunnel!", Snackbar.LENGTH_SHORT).show();
Expand All @@ -230,11 +223,11 @@ public void onMilestoneEvent(RouteProgress routeProgress, String instruction, Mi
Snackbar.make(contentLayout, instruction, Snackbar.LENGTH_SHORT).show();
}
instructionView.updateBannerInstructionsWith(milestone);
Timber.d("onMilestoneEvent - Current Instruction: " + instruction);
Timber.d("onMilestoneEvent - Current Instruction: %s", instruction);
}

@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
public void onResponse(@NonNull Call<DirectionsResponse> call, @NonNull Response<DirectionsResponse> response) {
Timber.d(call.request().url().toString());
if (response.body() != null) {
if (!response.body().routes().isEmpty()) {
Expand All @@ -249,14 +242,19 @@ public void onResponse(Call<DirectionsResponse> call, Response<DirectionsRespons
}

@Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
public void onFailure(@NonNull Call<DirectionsResponse> call, @NonNull Throwable throwable) {
Timber.e(throwable);
}

private void getRoute(Point origin, Point destination, Float bearing) {
Double heading = bearing == null ? null : bearing.doubleValue();
void updateLocation(Location location) {
if (!tracking) {
mapboxMap.getLocationComponent().forceLocationUpdate(location);
}
}

private void getRoute(Point origin, Point destination) {
NavigationRoute.builder(this)
.origin(origin, heading, 90d)
.origin(origin)
.destination(destination)
.accessToken(Mapbox.getAccessToken())
.build().getRoute(this);
Expand All @@ -281,16 +279,19 @@ private void drawRoute(DirectionsRoute route) {
}
}

private void resetLocationEngine(Point point) {
mockLocationEngine.moveTo(point);
navigation.setLocationEngine(mockLocationEngine);
}

private void resetLocationEngine(DirectionsRoute directionsRoute) {
mockLocationEngine.deactivate();
mockLocationEngine.assign(directionsRoute);
navigation.setLocationEngine(mockLocationEngine);
}

private void shutdownLocationEngine() {
if (mockLocationEngine != null) {
mockLocationEngine.removeLocationEngineListener(this);
mockLocationEngine.removeLocationUpdates();
mockLocationEngine.deactivate();
mockLocationEngine.removeLocationUpdates(callback);
}
}

Expand All @@ -299,4 +300,30 @@ private void shutdownNavigation() {
navigation.removeProgressChangeListener(this);
navigation.onDestroy();
}

private static class RerouteActivityLocationCallback implements LocationEngineCallback<LocationEngineResult> {

private final WeakReference<RerouteActivity> activityWeakReference;

RerouteActivityLocationCallback(RerouteActivity activity) {
this.activityWeakReference = new WeakReference<>(activity);
}

@Override
public void onSuccess(LocationEngineResult result) {
RerouteActivity activity = activityWeakReference.get();
if (activity != null) {
Location location = result.getLastLocation();
if (location == null) {
return;
}
activity.updateLocation(location);
}
}

@Override
public void onFailure(@NonNull Exception exception) {
Timber.e(exception);
}
}
}
Loading