summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLDownstreamTestApp/src/main/java/com/mapbox/mapboxsdk/downstream/testapp/MainActivity.java
blob: b7144eca35c392e3151275ba9a7931c49abb43a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.mapbox.mapboxsdk.downstream.testapp;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.MapboxDirections;
import com.mapbox.api.directions.v5.models.DirectionsResponse;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
import com.mapbox.services.android.navigation.ui.v5.route.OnRouteSelectionChangeListener;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import timber.log.Timber;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,
  MapboxMap.OnMapClickListener, Callback<DirectionsResponse>, OnRouteSelectionChangeListener {

  private static final String DIRECTIONS_RESPONSE = "directions-route.json";

  private MapView mapView;
  private TextView primaryRouteIndexTextView;

  private MapboxMap mapboxMap;
  private NavigationMapRoute navigationMapRoute;
  private StyleCycle styleCycle = new StyleCycle();

  private Marker originMarker;
  private Marker destinationMarker;
  private boolean alternativesVisible = true;
  private List<DirectionsRoute> routes = new ArrayList<>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Mapbox.getInstance(this, getString(R.string.mapbox_access_token));

    setContentView(R.layout.activity_main);

    primaryRouteIndexTextView = findViewById(R.id.primaryRouteIndexTextView);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.setStyleUrl(styleCycle.getStyle());
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);

    findViewById(R.id.fabStyles).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mapboxMap != null) {
          mapboxMap.setStyleUrl(styleCycle.getNextStyle());
        }
      }
    });

    findViewById(R.id.fabToggleAlternatives).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        alternativesVisible = !alternativesVisible;
        if (navigationMapRoute != null) {
          navigationMapRoute.showAlternativeRoutes(alternativesVisible);
        }
      }
    });
  }

  @Override
  public void onNewPrimaryRouteSelected(DirectionsRoute directionsRoute) {
    primaryRouteIndexTextView.setText(String.valueOf(routes.indexOf(directionsRoute)));
  }

  @Override
  public void onMapReady(MapboxMap mapboxMap) {
    this.mapboxMap = mapboxMap;
    navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap,
      "admin-3-4-boundaries-bg");
    Gson gson = new GsonBuilder().registerTypeAdapterFactory(DirectionsAdapterFactory.create())
      .create();
    DirectionsResponse response = gson.fromJson(loadJsonFromAsset(DIRECTIONS_RESPONSE),
      DirectionsResponse.class);
    navigationMapRoute.addRoute(response.routes().get(0));
    mapboxMap.addOnMapClickListener(this);
    navigationMapRoute.setOnRouteSelectionChangeListener(this);
  }

  @Override
  public void onMapClick(@NonNull LatLng point) {
    if (originMarker == null) {
      originMarker = mapboxMap.addMarker(new MarkerOptions().position(point));
    } else if (destinationMarker == null) {
      destinationMarker = mapboxMap.addMarker(new MarkerOptions().position(point));
      Point originPoint = Point.fromLngLat(
        originMarker.getPosition().getLongitude(), originMarker.getPosition().getLatitude());
      Point destinationPoint = Point.fromLngLat(
        destinationMarker.getPosition().getLongitude(), destinationMarker.getPosition().getLatitude());
      requestDirectionsRoute(originPoint, destinationPoint);
      mapboxMap.removeMarker(originMarker);
      mapboxMap.removeMarker(destinationMarker);
    } else {
      originMarker = null;
      destinationMarker = null;
      navigationMapRoute.removeRoute();
    }
  }

//  @Override
//  public void onMapLongClick(@NonNull LatLng point) {
//
//  }

  public void requestDirectionsRoute(Point origin, Point destination) {
    MapboxDirections directions = MapboxDirections.builder()
      .origin(origin)
      .destination(destination)
      .accessToken(Mapbox.getAccessToken())
      .profile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC)
      .overview(DirectionsCriteria.OVERVIEW_FULL)
      .annotations(DirectionsCriteria.ANNOTATION_CONGESTION)
      .alternatives(true)
      .steps(true)
      .build();

    directions.enqueueCall(this);
  }

  @Override
  public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
    if (response.body() != null && !response.body().routes().isEmpty()) {
      List<DirectionsRoute> routes = response.body().routes();
      this.routes = routes;
      navigationMapRoute.addRoutes(routes);
    }
  }

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

  @Override
  public void onResume() {
    super.onResume();
    mapView.onResume();
  }

  @Override
  protected void onStart() {
    super.onStart();
    mapView.onStart();
  }

  @Override
  protected void onStop() {
    super.onStop();
    mapView.onStop();
  }

  @Override
  public void onPause() {
    super.onPause();
    mapView.onPause();
  }

  @Override
  public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
  }

  public NavigationMapRoute getNavigationMapRoute() {
    return navigationMapRoute;
  }

  private String loadJsonFromAsset(String filename) {
    // Using this method to load in GeoJSON files from the assets folder.

    try {
      InputStream is = getAssets().open(filename);
      int size = is.available();
      byte[] buffer = new byte[size];
      is.read(buffer);
      is.close();
      return new String(buffer, "UTF-8");

    } catch (IOException ex) {
      ex.printStackTrace();
      return null;
    }
  }

  private static class StyleCycle {
    private static final String[] STYLES = new String[] {
      Style.MAPBOX_STREETS,
      Style.OUTDOORS,
      Style.LIGHT,
      Style.DARK,
      Style.SATELLITE_STREETS
    };

    private int index;

    private String getNextStyle() {
      index++;
      if (index == STYLES.length) {
        index = 0;
      }
      return getStyle();
    }

    private String getStyle() {
      return STYLES[index];
    }
  }
}