I am building an Android app that will render a map. Using the phone location and a specific destination, I need to display the route on the map, and re-draw the route when the user's phone is moving. Sorry for the long post, I am trying to make this as explicit as possible.
I have to use Azure Maps, and the user has to follow the original route, so I need to re-draw the route when the user is following it. Basically I just need to make the original route start from the phone location everytime the user moves.
I am getting the latitude and longitude of the phone every three seconds, and when the user picks a destination location, I am calling the Azure API for the route, which returns a JSON array that contains points(latitude and longitude) from origin to destination. I save these points in an ArrayList and draw the route using a LineString and a LineLayer. My problem is that I don't want to call the Azure Api for the route every three seconds or even everytime the phone moves, because the call is really slow, and it puts to much pressure on my backend server.
Considering that the JSON array is sorted from origin to destination, I tried to compute the absolute difference between the phone location and the points from the array, and save the point for which the difference is smaller than a threshold, because it's the closest point from the route to the phone actual location. Every three seconds I am computing this difference and save the point as the start from the new route, and re-draw the route. The code for the difference is this:
private void getStartIndices() {
//r_points is the ArrayList with the points from the route
if(r_points != null)
{
for (int i = 0; i < r_points.length() - 1; i++) {
try {
JSONObject point = r_points.getJSONObject(i);
if( abs(mCurrentLocation.getLongitude() - point.getDouble("longitude")) < 0.00001 && abs(mCurrentLocation.getLatitude() - point.getDouble("latitude")) < 0.00001 )
{
Log.e(TAG, i + " They both change");
returnable = i;
}
else{
if( abs(mCurrentLocation.getLongitude() - point.getDouble("longitude")) < 0.000004 )
{
Log.e(TAG,i + "The longitude is changing");
returnable = i;
}
else{
if(abs(mCurrentLocation.getLatitude() - point.getDouble("latitude")) < 0.000004 )
{
Log.e(TAG,i + " The latitude is changing");
returnable = i;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
This works but not accurate enough and it has some really bad bugs that I can't solve.
My question is: Is there any better way to do this, I really need to make as few calls as possible and I also need to make this redraw as accurate as possible.