55import android .os .Bundle ;
66import android .support .v7 .app .AppCompatActivity ;
77import android .support .v7 .widget .Toolbar ;
8- import android .text .TextUtils ;
98import android .util .Log ;
10- import android .widget .SeekBar ;
11- import android .widget .TextView ;
129
1310import com .mapbox .mapboxsdk .annotations .Polyline ;
1411import com .mapbox .mapboxsdk .annotations .PolylineOptions ;
1512import com .mapbox .mapboxsdk .geometry .LatLng ;
1613import com .mapbox .mapboxsdk .maps .MapView ;
1714import com .mapbox .mapboxsdk .maps .MapboxMap ;
1815import com .mapbox .mapboxsdk .maps .OnMapReadyCallback ;
16+ import com .mapbox .services .Constants ;
1917import com .mapbox .services .android .testapp .R ;
2018import com .mapbox .services .android .testapp .Utils ;
2119import com .mapbox .services .api .ServicesException ;
20+ import com .mapbox .services .api .mapmatching .v5 .MapMatchingCriteria ;
21+ import com .mapbox .services .api .mapmatching .v5 .MapboxMapMatching ;
22+ import com .mapbox .services .api .mapmatching .v5 .models .MapMatchingResponse ;
23+ import com .mapbox .services .commons .geojson .FeatureCollection ;
2224import com .mapbox .services .commons .geojson .LineString ;
2325import com .mapbox .services .commons .models .Position ;
24- import com .mapbox .services .api .mapmatching .v4 .MapMatchingCriteria ;
25- import com .mapbox .services .api .mapmatching .v4 .MapboxMapMatching ;
26- import com .mapbox .services .api .mapmatching .v4 .models .MapMatchingResponse ;
27-
28- import org .json .JSONArray ;
29- import org .json .JSONObject ;
26+ import com .mapbox .services .commons .utils .PolylineUtils ;
3027
3128import java .io .BufferedReader ;
3229import java .io .InputStream ;
@@ -45,7 +42,6 @@ public class MapMatchingActivity extends AppCompatActivity {
4542
4643 private MapView mapView ;
4744 private MapboxMap map ;
48- private LineString lineString ;
4945 private Polyline mapMatchedRoute ;
5046
5147 @ Override
@@ -58,42 +54,13 @@ protected void onCreate(Bundle savedInstanceState) {
5854
5955 getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
6056
61- final SeekBar precisionSeekBar = (SeekBar ) findViewById (R .id .seekbar_precision );
62- final TextView precisionTextView = (TextView ) findViewById (R .id .precision_value );
63- if (precisionSeekBar != null ) {
64- precisionSeekBar .setOnSeekBarChangeListener (new SeekBar .OnSeekBarChangeListener () {
65- @ Override
66- public void onProgressChanged (SeekBar seekBar , int progress , boolean fromUser ) {
67- if (lineString != null ) {
68- if (precisionTextView != null ) {
69- precisionTextView .setText (String .valueOf (progress ));
70- }
71- drawMapMatched (lineString , progress );
72- }
73- }
74-
75- @ Override
76- public void onStartTrackingTouch (SeekBar seekBar ) {
77-
78- }
79-
80- @ Override
81- public void onStopTrackingTouch (SeekBar seekBar ) {
82-
83- }
84- });
85- }
86-
8757 mapView = (MapView ) findViewById (R .id .mapView );
8858 mapView .onCreate (savedInstanceState );
8959 mapView .getMapAsync (new OnMapReadyCallback () {
9060 @ Override
9161 public void onMapReady (MapboxMap mapboxMap ) {
92-
9362 map = mapboxMap ;
94-
9563 new DrawGeoJson ().execute ();
96-
9764 }
9865 });
9966 }
@@ -157,27 +124,9 @@ protected List<Position> doInBackground(Void... voids) {
157124 }
158125
159126 inputStream .close ();
160-
161- // Parse JSON
162- JSONObject json = new JSONObject (sb .toString ());
163- JSONArray features = json .getJSONArray ("features" );
164- JSONObject feature = features .getJSONObject (0 );
165- JSONObject geometry = feature .getJSONObject ("geometry" );
166- if (geometry != null ) {
167- String type = geometry .getString ("type" );
168-
169- // Our GeoJSON only has one feature: a line string
170- if (!TextUtils .isEmpty (type ) && type .equalsIgnoreCase ("LineString" )) {
171-
172- // Get the Coordinates
173- JSONArray coords = geometry .getJSONArray ("coordinates" );
174- for (int lc = 0 ; lc < coords .length (); lc ++) {
175- JSONArray coord = coords .getJSONArray (lc );
176- Position position = Position .fromCoordinates (coord .getDouble (0 ), coord .getDouble (1 ));
177- points .add (position );
178- }
179- }
180- }
127+ FeatureCollection featureCollection = FeatureCollection .fromJson (sb .toString ());
128+ LineString lineString = (LineString ) featureCollection .getFeatures ().get (0 ).getGeometry ();
129+ points = lineString .getCoordinates ();
181130 } catch (Exception exception ) {
182131 Log .e (TAG , "Exception Loading GeoJSON: " + exception .toString ());
183132 }
@@ -188,18 +137,14 @@ protected List<Position> doInBackground(Void... voids) {
188137 @ Override
189138 protected void onPostExecute (List <Position > points ) {
190139 super .onPostExecute (points );
191-
192140 drawBeforeMapMatching (points );
193141
194- // Convert the route to a linestring
195- lineString = LineString .fromCoordinates (points );
196- drawMapMatched (lineString , 8 );
197-
142+ Position [] coordinates = new Position [points .size ()];
143+ drawMapMatched (points .toArray (coordinates ));
198144 }
199145 }
200146
201147 private void drawBeforeMapMatching (List <Position > points ) {
202-
203148 LatLng [] pointsArray = new LatLng [points .size ()];
204149 for (int i = 0 ; i < points .size (); i ++) {
205150 pointsArray [i ] = new LatLng (points .get (i ).getLatitude (), points .get (i ).getLongitude ());
@@ -212,36 +157,35 @@ private void drawBeforeMapMatching(List<Position> points) {
212157 .width (4 ));
213158 }
214159
215- private void drawMapMatched (LineString lineString , int precision ) {
160+ private void drawMapMatched (Position [] coordinates ) {
216161 try {
217162 // Setup the request using a client.
218163 MapboxMapMatching client = new MapboxMapMatching .Builder ()
219164 .setAccessToken (Utils .getMapboxAccessToken (MapMatchingActivity .this ))
220165 .setProfile (MapMatchingCriteria .PROFILE_DRIVING )
221- .setGpsPrecison (precision )
222- .setTrace (lineString )
166+ .setCoordinates (coordinates )
223167 .build ();
224168
225169 // Execute the API call and handle the response.
226170 client .enqueueCall (new Callback <MapMatchingResponse >() {
227171 @ Override
228172 public void onResponse (Call <MapMatchingResponse > call , Response <MapMatchingResponse > response ) {
229-
230173 // Create a new list to store the map matched coordinates.
231174 List <LatLng > mapMatchedPoints = new ArrayList <>();
232175
233176 // Check that the map matching API response is "OK".
234177 if (response .code () == 200 ) {
235178 // Convert the map matched response list from position to latlng coordinates.
236- Position [] positions = response .body ().getMatchedPoints ();
179+ String geometry = response .body ().getMatchings ().get (0 ).getGeometry ();
180+ List <Position > positions = PolylineUtils .decode (geometry , Constants .OSRM_PRECISION_V4 );
237181 if (positions == null ) {
238182 return ;
239183 }
240184
241- for (int i = 0 ; i < positions .length ; i ++) {
185+ for (int i = 0 ; i < positions .size () ; i ++) {
242186 mapMatchedPoints .add (new LatLng (
243- positions [ i ] .getLatitude (),
244- positions [ i ] .getLongitude ()));
187+ positions . get ( i ) .getLatitude (),
188+ positions . get ( i ) .getLongitude ()));
245189 }
246190
247191 if (mapMatchedRoute != null ) {
@@ -255,7 +199,7 @@ public void onResponse(Call<MapMatchingResponse> call, Response<MapMatchingRespo
255199 .width (4 ));
256200 } else {
257201 // If the response code does not response "OK" an error has occurred.
258- Log .e (TAG , "Too many coordinates, Profile not found, invalid input, or no match" );
202+ Log .e (TAG , "Too many coordinates, profile not found, invalid input, or no match. " );
259203 }
260204 }
261205
0 commit comments