|
| 1 | +package com.mapbox.services.android.testapp.directions; |
| 2 | + |
| 3 | +import android.graphics.Color; |
| 4 | +import android.graphics.drawable.Drawable; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.support.annotation.NonNull; |
| 7 | +import android.support.design.widget.FloatingActionButton; |
| 8 | +import android.support.design.widget.Snackbar; |
| 9 | +import android.support.v4.content.ContextCompat; |
| 10 | +import android.support.v7.app.AppCompatActivity; |
| 11 | +import android.support.v7.widget.Toolbar; |
| 12 | +import android.util.Log; |
| 13 | +import android.view.View; |
| 14 | +import android.widget.Toast; |
| 15 | + |
| 16 | +import com.mapbox.mapboxsdk.annotations.Icon; |
| 17 | +import com.mapbox.mapboxsdk.annotations.IconFactory; |
| 18 | +import com.mapbox.mapboxsdk.annotations.Marker; |
| 19 | +import com.mapbox.mapboxsdk.annotations.MarkerOptions; |
| 20 | +import com.mapbox.mapboxsdk.annotations.MarkerViewOptions; |
| 21 | +import com.mapbox.mapboxsdk.annotations.Polyline; |
| 22 | +import com.mapbox.mapboxsdk.annotations.PolylineOptions; |
| 23 | +import com.mapbox.mapboxsdk.camera.CameraPosition; |
| 24 | +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; |
| 25 | +import com.mapbox.mapboxsdk.constants.Style; |
| 26 | +import com.mapbox.mapboxsdk.geometry.LatLng; |
| 27 | +import com.mapbox.mapboxsdk.maps.MapView; |
| 28 | +import com.mapbox.mapboxsdk.maps.MapboxMap; |
| 29 | +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; |
| 30 | +import com.mapbox.services.Constants; |
| 31 | +import com.mapbox.services.android.testapp.MainActivity; |
| 32 | +import com.mapbox.services.android.testapp.R; |
| 33 | +import com.mapbox.services.android.testapp.Utils; |
| 34 | +import com.mapbox.services.commons.ServicesException; |
| 35 | +import com.mapbox.services.commons.geojson.LineString; |
| 36 | +import com.mapbox.services.commons.models.Position; |
| 37 | +import com.mapbox.services.commons.turf.TurfException; |
| 38 | +import com.mapbox.services.commons.utils.PolylineUtils; |
| 39 | +import com.mapbox.services.directions.v5.DirectionsCriteria; |
| 40 | +import com.mapbox.services.directions.v5.MapboxDirections; |
| 41 | +import com.mapbox.services.directions.v5.models.DirectionsResponse; |
| 42 | +import com.mapbox.services.directions.v5.models.DirectionsRoute; |
| 43 | +import com.mapbox.services.directions.v5.models.LegStep; |
| 44 | +import com.mapbox.services.directions.v5.models.RouteLeg; |
| 45 | +import com.mapbox.services.navigation.v5.RouteUtils; |
| 46 | + |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.List; |
| 49 | +import java.util.Locale; |
| 50 | + |
| 51 | +import retrofit2.Call; |
| 52 | +import retrofit2.Callback; |
| 53 | +import retrofit2.Response; |
| 54 | + |
| 55 | +public class RouteUtilsV5Activity extends AppCompatActivity implements OnMapReadyCallback { |
| 56 | + |
| 57 | + private final static String LOG_TAG = "RouteUtilsV5Activity"; |
| 58 | + |
| 59 | + private MapView mapView = null; |
| 60 | + private MapboxMap mapboxMap = null; |
| 61 | + |
| 62 | + private LatLng from = null; |
| 63 | + private LatLng to = null; |
| 64 | + private DirectionsRoute currentRoute = null; |
| 65 | + |
| 66 | + private Icon tapIcon; |
| 67 | + private Marker userTap = null; |
| 68 | + private List<Polyline> snapLines = null; |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void onCreate(Bundle savedInstanceState) { |
| 72 | + super.onCreate(savedInstanceState); |
| 73 | + setContentView(R.layout.activity_route_utils_v5); |
| 74 | + |
| 75 | + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 76 | + setSupportActionBar(toolbar); |
| 77 | + |
| 78 | + getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 79 | + |
| 80 | + // Create an Icon object for the marker to use |
| 81 | + IconFactory iconFactory = IconFactory.getInstance(this); |
| 82 | + Drawable iconDrawable = ContextCompat.getDrawable(this, R.drawable.ic_my_location_black_24dp); |
| 83 | + tapIcon = iconFactory.fromDrawable(iconDrawable); |
| 84 | + |
| 85 | + // Set up a standard Mapbox map |
| 86 | + mapView = (MapView) findViewById(R.id.mapview); |
| 87 | + mapView.onCreate(savedInstanceState); |
| 88 | + mapView.getMapAsync(this); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void onMapReady(MapboxMap mapboxMap) { |
| 93 | + this.mapboxMap = mapboxMap; |
| 94 | + |
| 95 | + mapboxMap.setStyleUrl(Style.MAPBOX_STREETS); |
| 96 | + |
| 97 | + // Dupont Circle |
| 98 | + LatLng target = new LatLng(38.90962, -77.04341); |
| 99 | + |
| 100 | + // Move map |
| 101 | + CameraPosition cameraPosition = new CameraPosition.Builder() |
| 102 | + .target(target) |
| 103 | + .zoom(14) |
| 104 | + .build(); |
| 105 | + mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); |
| 106 | + |
| 107 | + mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() { |
| 108 | + @Override |
| 109 | + public void onMapClick(@NonNull LatLng point) { |
| 110 | + if (from == null) { |
| 111 | + setFrom(point); |
| 112 | + } else if (to == null) { |
| 113 | + setTo(point); |
| 114 | + } else { |
| 115 | + try { |
| 116 | + doUtils(point); |
| 117 | + } catch (ServicesException e) { |
| 118 | + Log.e(LOG_TAG, "Services exception: " + e.getMessage()); |
| 119 | + e.printStackTrace(); |
| 120 | + } catch (TurfException e) { |
| 121 | + Log.e(LOG_TAG, "Turf exception: " + e.getMessage()); |
| 122 | + e.printStackTrace(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + private void setFrom(LatLng point) { |
| 130 | + from = point; |
| 131 | + mapboxMap.addMarker(new MarkerOptions() |
| 132 | + .position(point) |
| 133 | + .title("From")); |
| 134 | + } |
| 135 | + |
| 136 | + private void setTo(LatLng point) { |
| 137 | + to = point; |
| 138 | + mapboxMap.addMarker(new MarkerOptions() |
| 139 | + .position(point) |
| 140 | + .title("To")); |
| 141 | + |
| 142 | + try { |
| 143 | + getRoute(Position.fromCoordinates(from.getLongitude(), from.getLatitude()), Position.fromCoordinates(to.getLongitude(), to.getLatitude())); |
| 144 | + } catch (ServicesException e) { |
| 145 | + showMessage(e.getMessage()); |
| 146 | + e.printStackTrace(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void doUtils(LatLng point) throws ServicesException, TurfException { |
| 151 | + // Remove previous |
| 152 | + if (userTap != null) { |
| 153 | + mapboxMap.removeMarker(userTap); |
| 154 | + } |
| 155 | + |
| 156 | + userTap = mapboxMap.addMarker(new MarkerOptions().position(point).setIcon(tapIcon)); |
| 157 | + |
| 158 | + RouteUtils routeUtils = new RouteUtils(); |
| 159 | + RouteLeg route = currentRoute.getLegs().get(0); |
| 160 | + Position position = Position.fromCoordinates(point.getLongitude(), point.getLatitude()); |
| 161 | + |
| 162 | + // General situational message |
| 163 | + String message = String.format(Locale.US, "You're closest to step %d/%d (%s)", |
| 164 | + routeUtils.getClosestStep(position, route) + 1, |
| 165 | + route.getSteps().size(), |
| 166 | + routeUtils.isOffRoute(position, route) ? "off-route" : "not off-route"); |
| 167 | + showMessage(message); |
| 168 | + |
| 169 | + // Remove previous lines |
| 170 | + if (snapLines != null && snapLines.size() > 0) { |
| 171 | + for (Polyline snapLine: snapLines) { |
| 172 | + mapboxMap.removePolyline(snapLine); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Draw snap to route lines |
| 177 | + snapLines = new ArrayList<>(); |
| 178 | + for (int stepIndex = 0; stepIndex < route.getSteps().size(); stepIndex++) { |
| 179 | + Position snapPoint = routeUtils.getSnapToRoute(position, route, stepIndex); |
| 180 | + LatLng[] points = new LatLng[] { |
| 181 | + point, |
| 182 | + new LatLng(snapPoint.getLatitude(), snapPoint.getLongitude())}; |
| 183 | + snapLines.add(mapboxMap.addPolyline(new PolylineOptions() |
| 184 | + .add(points) |
| 185 | + .color(Color.parseColor("#f9886c")) |
| 186 | + .width(2))); |
| 187 | + } |
| 188 | + |
| 189 | + // Log some extra info |
| 190 | + for (int stepIndex = 0; stepIndex < route.getSteps().size(); stepIndex++) { |
| 191 | + Log.d(LOG_TAG, String.format("Step %d: in step = %b, distance = %.1fkm", |
| 192 | + stepIndex + 1, |
| 193 | + routeUtils.isInStep(position, route, stepIndex), |
| 194 | + routeUtils.getDistanceToStep(position, route, stepIndex))); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private void getRoute(Position origin, Position destination) throws ServicesException { |
| 199 | + ArrayList<Position> positions = new ArrayList<>(); |
| 200 | + positions.add(origin); |
| 201 | + positions.add(destination); |
| 202 | + |
| 203 | + MapboxDirections client = new MapboxDirections.Builder() |
| 204 | + .setAccessToken(Utils.getMapboxAccessToken(this)) |
| 205 | + .setCoordinates(positions) |
| 206 | + .setProfile(DirectionsCriteria.PROFILE_DRIVING) |
| 207 | + .setSteps(true) |
| 208 | + .setOverview(DirectionsCriteria.OVERVIEW_FULL) |
| 209 | + .build(); |
| 210 | + |
| 211 | + client.enqueueCall(new Callback<DirectionsResponse>() { |
| 212 | + @Override |
| 213 | + public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) { |
| 214 | + // You can get generic HTTP info about the response |
| 215 | + Log.d(LOG_TAG, "Response code: " + response.code()); |
| 216 | + if (response.body() == null) { |
| 217 | + Log.e(LOG_TAG, "No routes found, make sure you set the right user and access token."); |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + // Print some info about the route |
| 222 | + currentRoute = response.body().getRoutes().get(0); |
| 223 | + Log.d(LOG_TAG, "Distance: " + currentRoute.getDistance()); |
| 224 | + showMessage(String.format(Locale.US, "Route has %d steps and it's %.1f meters long.", |
| 225 | + currentRoute.getLegs().get(0).getSteps().size(), |
| 226 | + currentRoute.getDistance())); |
| 227 | + |
| 228 | + // Draw the route on the map |
| 229 | + drawRoute(currentRoute); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void onFailure(Call<DirectionsResponse> call, Throwable t) { |
| 234 | + Log.e(LOG_TAG, "Error: " + t.getMessage()); |
| 235 | + showMessage("Error: " + t.getMessage()); |
| 236 | + } |
| 237 | + }); |
| 238 | + } |
| 239 | + |
| 240 | + private void drawRoute(DirectionsRoute route) { |
| 241 | + // We're gonna draw each step in an alternating color |
| 242 | + String[] colors = new String[] {"#3887be", "#56b881"}; // Blue, green |
| 243 | + |
| 244 | + List<Position> coordinates; |
| 245 | + LatLng[] points; |
| 246 | + int colorIndex = 0; |
| 247 | + for (int i = 0; i < route.getLegs().get(0).getSteps().size(); i++) { |
| 248 | + LegStep step = route.getLegs().get(0).getSteps().get(i); |
| 249 | + coordinates = PolylineUtils.decode(step.getGeometry(), Constants.OSRM_PRECISION_V5); |
| 250 | + points = new LatLng[coordinates.size()]; |
| 251 | + for (int j = 0; j < coordinates.size(); j++) { |
| 252 | + points[j] = new LatLng( |
| 253 | + coordinates.get(j).getLatitude(), |
| 254 | + coordinates.get(j).getLongitude()); |
| 255 | + } |
| 256 | + |
| 257 | + colorIndex ^= 1; |
| 258 | + mapboxMap.addPolyline(new PolylineOptions() |
| 259 | + .add(points) |
| 260 | + .color(Color.parseColor(colors[colorIndex])) |
| 261 | + .width(5)); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + private void showMessage(String message) { |
| 266 | + Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); |
| 267 | + } |
| 268 | + |
| 269 | + @Override |
| 270 | + public void onResume() { |
| 271 | + super.onResume(); |
| 272 | + mapView.onResume(); |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public void onPause() { |
| 277 | + super.onPause(); |
| 278 | + mapView.onPause(); |
| 279 | + } |
| 280 | + |
| 281 | + @Override |
| 282 | + protected void onSaveInstanceState(Bundle outState) { |
| 283 | + super.onSaveInstanceState(outState); |
| 284 | + mapView.onSaveInstanceState(outState); |
| 285 | + } |
| 286 | + |
| 287 | + @Override |
| 288 | + protected void onDestroy() { |
| 289 | + super.onDestroy(); |
| 290 | + mapView.onDestroy(); |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public void onLowMemory() { |
| 295 | + super.onLowMemory(); |
| 296 | + mapView.onLowMemory(); |
| 297 | + } |
| 298 | +} |
0 commit comments