package com jtaxi services providers import android location Address i

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.jtaxi.services.providers;
import android.location.Address;
import com.jtaxi.ProjectConfig;
import com.jtaxi.helper.Console;
import com.jtaxi.services.http.HttpClientProvider;
import com.jtaxi.services.http.HttpRequestContainer;
import com.jtaxi.services.http.InternalJsonResponseHandler;
import com.jtaxi.services.http.RequestAction;
import com.jtaxi.services.http.client.RequestParams;
import org.json.JSONArray;
import org.json.JSONObject;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* User: Roman Yefimchuk
* Date: 1/3/14
* Time: 1:44 PM
*/
public class RouteDetailsProvider {
private static final boolean ROUNDING_RESULT = false;
private static final String TAG = "Route Details Provider";
private static final String REQUEST_URl = "http://maps.googleapis.com/maps/api/directions/json";
private static final String ORIGIN = "origin";
private static final String DESTINATION = "destination";
private static final String SENSOR = "sensor";
private static final String STATUS = "status";
private static final String ERROR_MESSAGE = "error_message";
private static final String STATUS_OK = "OK";
private static final String ROUTES = "routes";
private static final String LEGS = "legs";
private static final String DISTANCE = "distance";
private static final String DURATION = "duration";
private static final String VALUE = "value";
private static final int INDEX = 0;
private static final double MINUTE_FACTOR = 0.0166666667;
private static final double MILES_FACTOR = 0.000621371192;
private static final int SCALE_FACTOR = 2;
private RouteDetailsProvider() {
}
private static String formatPoint(Address address) {
return address.getLatitude() + "," + address.getLongitude();
}
private static double roundValue(double input) {
BigDecimal decimal = new BigDecimal(input);
BigDecimal output = decimal.setScale(SCALE_FACTOR, RoundingMode.UP);
return output.doubleValue();
}
public static RequestAction obtainRouteDetails(final Address origin, final Address destination, final Callback callback) {
return HttpClientProvider.sendHttpRequest(new HttpRequestContainer() {
@Override
public RequestAction sendRequest(HttpClientProvider.AsyncHttpClientWrapper client) {
final RequestParams params = new RequestParams();
params.put(ORIGIN, formatPoint(origin));
params.put(DESTINATION, formatPoint(destination));
params.put(SENSOR, String.valueOf(true));
return client.get(REQUEST_URl, params, new InternalJsonResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
try {
String status = response.getString(STATUS);
if (status.equals(STATUS_OK)) {
JSONArray routes = response.getJSONArray(ROUTES);
{
JSONObject routeItem = routes.getJSONObject(INDEX);
JSONArray legs = routeItem.getJSONArray(LEGS);
{
JSONObject legItem = legs.getJSONObject(INDEX);
JSONObject distance = legItem.getJSONObject(DISTANCE);
double miles = distance.getDouble(VALUE) * MILES_FACTOR;
JSONObject duration = legItem.getJSONObject(DURATION);
double minutes = duration.getDouble(VALUE) * MINUTE_FACTOR;
if (callback != null) {
if (ROUNDING_RESULT) {
miles = roundValue(miles);
minutes = roundValue(minutes);
}
callback.onSuccess(miles, minutes);
}
}
}
} else {
String errorMessage = response.getString(ERROR_MESSAGE);
if (callback != null) {
callback.onFailure(errorMessage);
}
}
} catch (Exception e) {
if (ProjectConfig.DEBUG) {
Console.d(TAG, "URL: " + REQUEST_URl + "?" + params);
e.printStackTrace();
}
if (callback != null) {
callback.onFailure(e.getMessage());
}
}
}
@Override
public void onFailure(String message) {
if (callback != null) {
callback.onFailure(message);
}
}
}.toAsyncHandler());
}
});
}
public static interface Callback {
public void onSuccess(double miles, double minutes);
public void onFailure(String message);
}
}