Mapbox Android Services is a great way to consume Mapbox APIs in your Java and Android applications. With just a few lines of code, you’ve been able to get the most efficient routes using the directions API and location information with the geocoder API. Now with the release of 1.2 we are bundling even more Mapbox APIs into a single SDK for use in your Android applications.
Map matching
If your apps are consuming GPS location information such as the tracking of a car, bicycle or person walking, the coordinates returned probably aren’t going to match up with the roads or pathways. This isn’t ideal when you want to draw the route on a map and will often result in a route that looks like this:
With the map matching API, you can pass in your route as a linestring and it will return the same route only now it will follow along the roads and pathways. Here’s a snippet you can include in your app that uses a list of coordinates originally from a car GPS:
lineString=LineString.fromCoordinates(<passinyourlistofcoordinateshere>);// Setup the request using a client.MapboxMapMatchingclient=newMapboxMapMatching.Builder().setAccessToken(<youraccesstokenhere>).setProfile(DirectionsCriteria.PROFILE_DRIVING).setGpsPrecison(8).setTrace(lineString).build();// Execute the API call and handle the response.client.enqueueCall(newCallback<MapMatchingResponse>(){@OverridepublicvoidonResponse(Call<MapMatchingResponse>call,Response<MapMatchingResponse>response){// Create a new list to store the map matched coordinates.List<LatLng>mapMatchedPoints=newArrayList<>();// Convert the map matched response list from position to latlng coordinates.for(inti=0;i<response.body().getMatchedPoints().size();i++){mapMatchedPoints.add(newLatLng(response.body().getMatchedPoints().get(i).getLatitude(),response.body().getMatchedPoints().get(i).getLongitude()));}// Add the map matched route to your Mapbox mapmap.addPolyline(newPolylineOptions().addAll(mapMatchedPoints).color(Color.parseColor("#3bb2d0")).width(4));}...
Here’s what the same polyline looks like after filtering it through the map matching API:
Route simplification
When route coordinates are acquired from a GPS, it is often done in intervals resulting in a route made up of too many points. This can be overkill when the route is a straight trajectory, and only a few points are needed to retain the routes shape. Mapbox Android Services now includes polyline simplification which reduces the number of points in a polyline while retaining its shape. This can lead to a boost in performance and a reduction in visual noise.
To simplify a list of coordinates, use the simplify method to pass in the points, a tolerance value, and whether the quality should be at it’s highest or not:
Position[]before=newPosition[points.size()];for(inti=0;i<points.size();i++)before[i]=points.get(i);Position[]after=PolylineUtils.simplify(before,tolerance,quality);
Other utils using Turf
Turf brings powerful new methods to analyze and transform your map data on mobile. You can calculate bearing, distance, and even a destination just by giving a distance. If you need to extract a smaller section of a polyline or linestring, use Turf’s line slice method now found in the SDK.
Getting started
Along with the new features mentioned above, we’ve also made improvements to our geocoder which now serves up wiki data. Check out the overview page along with the new examples and begin working with Mapbox Android Services in your Android or Java application today!