Saturday 19 January 2019

TravelTimeCalculator: Tying it All Together

TravelTimeHandler is now working in the sense that it collects directions from Google Directions (or casched from the internal SQLite database) and select the quickest option.

Implementation
I started with skeleton code that indicated what it was supposed to do using printouts to the logs.


The stubbed method. I'll replace the
console print statements with actual
code later.
After having the overall structure in place, I implemented the needed methods:

  • scanDirectionsForTap is activated when the user taps the map. It starts with a query to the database for any directions between the center of the map and the tapped location. If no results are found, it queries Google Directions for bicycling, driving, transit and walking.
  • getShortestDirectionFromDb queries the database for the shortest duration (or distance) between two points. It returns the value and an integer representing the mode.
  • saveDirectionsToDb saves the directions to the database
  • clearMarkers will clear the markers from the map (stub). I'll call this method when the map is moved.
  • addMarkers will add a marker to the map (stub)
  • getDirectionsFromUrl queries Google Directions for distance and duration for a couple of coordinates and a mode of transport. The response is handled in the over-ridden method onResponse. onResponse adds the result to the database. After that, it will query the database for the shortest distance/duration using getShortestDirectionFromDb.
  • readDirectionsFromJson extracts the relevant data from the JSON response from Google Directions.

Since the database handles only integers for the coordinates, I'll multiply the coordinates by 1000 and round them to integers when querying the database. 

The output from some queries is:
Initially, the app had no direction data for the coordinates.
After the query, the database was populated with the data.

Context 
One issue that I saw was applicationContext. When having code that uses context in another file than the mainActivity.java file, the app can't find it. The solution was to get the app context explicitly:


Remaining Work
I want to visualize the results with markers. I'll add one marker in the middle that represents the current location.

When the user taps on the map, I'll add a corresponding marker on the map. The marker will have a color or symbol that indicates the quickest transport mode (driving, transit, walking or bicycling).

When the user moves the map, the existing markers are cleared. The app should look for existing directions in the database and add corresponding markers automatically.

No comments:

Post a Comment