1

I am using Open Trip Planner (OTP) in my Android application.

I want to know how to parse a trip request result to draw them on google maps as I found no references on OTP website

Response i get from a request is

<Response><requestParameters><date>03-25-2016</date>    <mode>TRANSIT,WALK</mode><arriveBy>false</arriveBy><wheelchair>false</wheelchair><fromPlace>30.09612809935851,31.28425598144531</fromPlace><toPlace>30.094494377942357,31.285972595214844</toPlace><time>7:57pm</time><maxWalkDistance>804.672</maxWalkDistance><locale>en</locale></requestParameters><plan><date>1458928620000</date><from><name>path</name><stopId/><stopCode/><platformCode/><lon>31.283823029209294</lon><lat>30.096246742635532</lat><arrival/><departure/><orig></orig><zoneId/><stopIndex/><stopSequence/><vertexType>NORMAL</vertexType></from><to><name>Ahmed Idris</name><stopId/><stopCode/><platformCode/><lon>31.285831330361862</lon><lat>30.094295136473086</lat><arrival/><departure/><orig></orig><zoneId/><stopIndex/><stopSequence/><vertexType>NORMAL</vertexType></to><itineraries><itineraries><duration>296</duration><startTime>1458928620000</startTime><endTime>1458928916000</endTime><walkTime>296</walkTime><transitTime>0</transitTime><waitingTime>0</waitingTime><walkDistance>372.00220300489866</walkDistance><walkLimitExceeded>false</walkLimitExceeded><elevationLost>0.0</elevationLost><elevationGained>0.0</elevationGained><transfers>0</transfers><fare/><legs><legs><startTime>1458928620000</startTime><endTime>1458928916000</endTime><departureDelay>0</departureDelay><arrivalDelay>0</arrivalDelay><realTime>false</realTime><isNonExactFrequency/><headway/><distance>371.92100000000005</distance><pathway>false</pathway><mode>WALK</mode><route></route><agencyName/><agencyUrl/><agencyTimeZoneOffset>7200000</agencyTimeZoneOffset><routeColor/><routeType/><routeId/><routeTextColor/><interlineWithPreviousLeg>false</interlineWithPreviousLeg><tripShortName/><tripBlockId/><headsign/><agencyId/><tripId/><serviceDate/><from><name>path</name><stopId/><stopCode/><platformCode/><lon>31.283823029209294</lon><lat>30.096246742635532</lat><arrival/><departure>1458928620000</departure><orig></orig><zoneId/><stopIndex/><stopSequence/><vertexType>NORMAL</vertexType></from><to><name>Ahmed Idris</name><stopId/><stopCode/><platformCode/><lon>31.285831330361862</lon><lat>30.094295136473086</lat><arrival>1458928916000</arrival><departure/><orig></orig><zoneId/><stopIndex/><stopSequence/><vertexType>NORMAL</vertexType></to><legGeometry><points>oduvD{b}}DnA\dBd@j@iBd@cAV[AG@a@GICM@GDGBEFA|AqD</points><levels/><length>15</length></legGeometry><routeShortName/><routeLongName/><boardRule/><alightRule/><rentedBike>false</rentedBike><duration>296.0</duration><transitLeg>false</transitLeg><steps><steps><distance>106.37</distance><relativeDirection>DEPART</relativeDirection><streetName>path</streetName><absoluteDirection>SOUTH</absoluteDirection><exit/><stayOn>false</stayOn><area>false</area><bogusName>false</bogusName><lon>31.283823029209294</lon><lat>30.096246742635532</lat><elevation/></steps><steps><distance>113.914</distance><relativeDirection>LEFT</relativeDirection><streetName>Yusef Attia</streetName><absoluteDirection>SOUTHEAST</absoluteDirection><exit/><stayOn>false</stayOn><area>false</area><bogusName>false</bogusName><lon>31.2834891</lon><lat>30.095334800000003</lat><elevation/></steps><steps><distance>20.445</distance><relativeDirection>LEFT</relativeDirection><streetName>Tereet Al-Gondy</streetName><absoluteDirection>EAST</absoluteDirection><exit/><stayOn>false</stayOn><area>false</area><bogusName>false</bogusName><lon>31.284490100000003</lon><lat>30.0948038</lat><elevation/></steps><steps><distance>31.328</distance><relativeDirection>CIRCLE_CLOCKWISE</relativeDirection><streetName>road</streetName><absoluteDirection>NORTHEAST</absoluteDirection><exit>2</exit><stayOn>false</stayOn><area>false</area><bogusName>true</bogusName><lon>31.2847003</lon><lat>30.0948011</lat><elevation/></steps><steps><distance>99.864</distance><relativeDirection>SLIGHTLY_LEFT</relativeDirection><streetName>Ahmed Idris</streetName><absoluteDirection>SOUTHEAST</absoluteDirection><exit/><stayOn>false</stayOn><area>false</area><bogusName>false</bogusName><lon>31.2849451</lon><lat>30.0947655</lat><elevation/></steps></steps></legs></legs><tooSloped>false</tooSloped></itineraries></itineraries></plan><error/><debugOutput><precalculationTime>1</precalculationTime><pathCalculationTime>0</pathCalculationTime><pathTimes><pathTimes>0</pathTimes></pathTimes><renderingTime>0</renderingTime><totalTime>1</totalTime><timedOut>false</timedOut></debugOutput></Response>

so how i can parse that response on google maps to draw the route like OTP doing with OSM

  • Are you using software or building something from scratch? Do you have code examples of what you are attempting? – MaryBeth Mar 25 '16 at 18:15
0

Check out our OpenTripPlanner for Android app on Github for a full working example.

We use Jackson for data-binding, which allows us to deserialize the JSON response from OpenTripPlanner directly to Plain Old Java Objects (POJOs). This allows you to avoid writing all the JSON parsing code. See the TripRequest.planTrip() method for an AsyncTask that makes a request to an OTP server.

Here's the basic code that makes the request to an OTP server and deserializes the JSON response into a Response object that mirrors the structure of the raw XML/JSON response:

String otpRequestUrl = "http://mobullity.forest.usf.edu/?module=planner&fromPlace=28.068685851955387%2C-82.40977764129639&toPlace=28.05936996446416%2C-82.42016315460205&time=11%3A25pm&date=03-25-2016&mode=TRANSIT%2CWALK&maxWalkDistance=482.8032&wheelchair=false&arriveBy=false&showIntermediateStops=true&itinIndex=0";
URL url = new URL(otpRequestUrl);

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setConnectTimeout(OTPApp.HTTP_CONNECTION_TIMEOUT);
urlConnection.setReadTimeout(OTPApp.HTTP_SOCKET_TIMEOUT);
Response plan = JacksonConfig.getObjectReaderInstance()
            .readValue(urlConnection.getInputStream());

The JacksonConfig class sets up the Jackson ObjectReader instance.

We've created an OTP POJOs project that you're free to use in your own application.

After you parse the response, you just use Android Maps API to draw the points and lines on the map, as you would for any mapping application. You can see how we do this in the main Activity.

As an aside, if you don't need to create your own Android app, but something is missing from our app that you need, we'd welcome contributions to our OTP Android app!

  • I'm using volley for requests so would it work ? with POST or GET ? – JenuRudan Mar 27 '16 at 17:01
  • I haven't used volley before, but if you can get a connection stream or the raw JSON from volley you should be able to pass this to Jackson and let Jackson deserialize it to Java objects. – Sean Barbeau Mar 27 '16 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.