Integrate HERE public transit Routing v8 RESTful API with HERE mSDK
It is often required to customize public transit routing experience within the HERE mSDK, either fetching more data, special rendering requirements, ...etc. so let's integrate HERE public transit routing RESTful API with HERE mSDK
N.B. you can find a working instance of a crash sample app HERE
* Import required android/java libs
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
Implement the logic to:
+ Send a RESTful API call to HERE public transit v8 service
+ Parse the JSON response
- Draw different transit routes (Each transit mode with different color)
Please refer to HERE flexible polyline HERE
- Draw different intermediate stops (Bus, subway, train, etc.)
- Attach stop name, and life departure time to each intermediate stop
public void requestPublicTransitRoute(){
String url = 'https://transit.router.hereapi.com/v8/routes?origin=25.0636444,55.2392576&destination=24.9604832,55.1508421&return=incidents,bookingLinks,travelSummary,actions,intermediate,fares,polyline&apikey=zqXX556h4qRUgZyA-PtL9CgTohrfxmFIwVSugJwou9w/'
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray routes = response.getJSONArray("routes");
//I will ONLY mind the very first route result
JSONObject route = routes.getJSONObject(0);
//retrieve route sections
JSONArray sections = route.getJSONArray("sections");
//loop route sections
for (int i = 0; i < sections.length(); i++) {
JSONObject section = sections.getJSONObject(i);
//retrieve transport object for the current section
JSONObject transport = section.getJSONObject("transport");
//decode the HERE flexible polyline
String flexiblePolyline = section.getString("polyline");
List flexibleCoordinates = m_flexiblePolylineEncoderDecoder.decode(flexiblePolyline);
List coordinates = new ArrayList();
for (int ii = 0; ii < flexibleCoordinates.size(); ii++) {
GeoCoordinate coordinate = new GeoCoordinate(flexibleCoordinates.get(ii).lat, flexibleCoordinates.get(ii).lng);
coordinates.add(coordinate);
}
//creaete a map polyline object from the decode flexibale polyline coordinates
MapPolyline mapPolyline = new MapPolyline(new GeoPolyline(coordinates));
//check if transit mode is SUBWAY, then add your own logic for relevant info, and map objects
if (transport.getString("mode").toUpperCase().equals("SUBWAY")){
//RED
mapPolyline.setLineColor(-65536);
JSONArray intermediateStops = section.getJSONArray("intermediateStops");
for (int iii = 0; iii < intermediateStops.length(); iii++) {
String intermediateStop_Lat = intermediateStops.getJSONObject(iii).getJSONObject("departure").getJSONObject("place").getJSONObject("location").getString("lat");
String intermediateStop_Lng = intermediateStops.getJSONObject(iii).getJSONObject("departure").getJSONObject("place").getJSONObject("location").getString("lng");
String intermediateStop_Name = intermediateStops.getJSONObject(iii).getJSONObject("departure").getJSONObject("place").getString("name");
// MapMarker mapMarker = new MapMarker(new GeoCoordinate(Double.valueOf(intermediateStop_Lat), Double.valueOf(intermediateStop_Lng)), new Image());
// mapObjectList.add(mapMarker);
MapLabeledMarker mapLabeledMarker = new MapLabeledMarker(new GeoCoordinate(Double.valueOf(intermediateStop_Lat), Double.valueOf(intermediateStop_Lng)));
mapLabeledMarker.setCoordinate(new GeoCoordinate(Double.valueOf(intermediateStop_Lat), Double.valueOf(intermediateStop_Lng)));
mapLabeledMarker.setIcon(IconCategory.METRO_STATION);
mapLabeledMarker.setTag(intermediateStops.getJSONObject(iii));
// mapLabeledMarker.setLabelText(intermediateStop_Name, intermediateStop_Name);
mapLabeledMarkerList.add(mapLabeledMarker);
}
}
//check if transit mode is BUS, then add your own logic for relevant info, and map objects
else if (transport.getString("mode").toUpperCase().equals("BUS")){
//YELLOW
mapPolyline.setLineColor(-256);
JSONArray intermediateStops = section.getJSONArray("intermediateStops");
for (int iii = 0; iii < intermediateStops.length(); iii++) {
String intermediateStop_Lat = intermediateStops.getJSONObject(iii).getJSONObject("departure").getJSONObject("place").getJSONObject("location").getString("lat");
String intermediateStop_Lng = intermediateStops.getJSONObject(iii).getJSONObject("departure").getJSONObject("place").getJSONObject("location").getString("lng");
String intermediateStop_Name = intermediateStops.getJSONObject(iii).getJSONObject("departure").getJSONObject("place").getString("name");
// MapMarker mapMarker = new MapMarker(new GeoCoordinate(Double.valueOf(intermediateStop_Lat), Double.valueOf(intermediateStop_Lng)), new Image());
// mapObjectList.add(mapMarker);
MapLabeledMarker mapLabeledMarker = new MapLabeledMarker(new GeoCoordinate(Double.valueOf(intermediateStop_Lat), Double.valueOf(intermediateStop_Lng)));
mapLabeledMarker.setCoordinate(new GeoCoordinate(Double.valueOf(intermediateStop_Lat), Double.valueOf(intermediateStop_Lng)));
mapLabeledMarker.setIcon(IconCategory.BUS_STATION);
mapLabeledMarker.setTag(intermediateStops.getJSONObject(iii));
// mapLabeledMarker.setLabelText(intermediateStop_Name, intermediateStop_Name);
mapLabeledMarkerList.add(mapLabeledMarker);
}
}
//check if transit mode is PEDESTRIAN, then add your own logic for relevant info, and map objects
else if (transport.getString("mode").toUpperCase().equals("PEDESTRIAN")){
//BLUE
mapPolyline.setLineColor(-16776961);}
mapPolyline.setLineWidth(13);
// mapObjectList.add(mapPolyline);
mapPolylineList.add(mapPolyline);
}
// m_map.addMapObjects(mapObjectList);
m_map.addMapObjects(mapPolylineList);
m_map.addMapObjects(mapLabeledMarkerList);
} catch (JSONException e) {
e.printStackTrace();
}
//textView.setText("Response: " + response.toString());
Log.d("WaliedCheetos: ", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
error.printStackTrace();
}
});
m_requestQueue.add(jsonObjectRequest);
}
- References
+ HERE public transit routing v8
+ HERE flexible polyline #1
+ HERE flexible polyline #2
+ Crash sample app HERE