package com jtaxi services providers import android graphics Color imp

  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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.jtaxi.services.providers;
import android.graphics.Color;
import android.location.Address;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
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.util.ArrayList;
import java.util.List;
/**
* User: Roman Yefimchuk
* Date: 1/4/14
* Time: 1:35 AM
*/
public class RouteWayProvider {
private static final String TAG = "Route Way Provider";
private static final float DEFAULT_WIDTH = 5;
private static final int DEFAULT_COLOR = Color.BLUE;
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 STEPS = "steps";
private static final String POLYLINE = "polyline";
private static final String POINTS = "points";
private static final int INDEX = 0;
private RouteWayProvider() {
}
private static String formatPoint(Address address) {
return address.getLatitude() + "," + address.getLongitude();
}
private static List<LatLng> decodePolyline(String points) {
int len = points.length();
int index = 0;
List<LatLng> decoded = new ArrayList<LatLng>();
int lat = 0;
int lng = 0;
while (index < len) {
int b;
int shift = 0;
int result = 0;
do {
b = points.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dLat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dLat;
shift = 0;
result = 0;
do {
b = points.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dLng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dLng;
LatLng latLng = new LatLng(lat / 100000d, lng / 100000d);
decoded.add(latLng);
}
return decoded;
}
public static void obtainRouteWay(final Address origin, final Address destination, final Callback callback) {
obtainRouteWay(origin, destination, 0, callback);
}
public static void obtainRouteWay(final Address origin, final Address destination, final int polylineWidth,
final Callback callback) {
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 legsItem = legs.getJSONObject(INDEX);
{
JSONArray steps = legsItem.getJSONArray(STEPS);
int length = steps.length();
List<LatLng> pointsList = new ArrayList<LatLng>();
for (int stepIndex = 0; stepIndex < length; stepIndex++) {
JSONObject step = steps.getJSONObject(stepIndex);
{
JSONObject polyline = step.getJSONObject(POLYLINE);
String points = polyline.getString(POINTS);
List<LatLng> decodedPoints = decodePolyline(points);
pointsList.addAll(decodedPoints);
}
}
PolylineOptions polylineOptions = null;
if (callback != null) {
if (pointsList.isEmpty()) {
callback.onSuccess(polylineOptions);
} else {
polylineOptions = new PolylineOptions();
polylineOptions.addAll(pointsList);
polylineOptions.color(DEFAULT_COLOR);
if (polylineWidth > 0) {
polylineOptions.width(polylineWidth);
} else {
polylineOptions.width(DEFAULT_WIDTH);
}
callback.onSuccess(polylineOptions);
}
}
}
}
}
} 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) {
String message = e.getMessage();
if (message != null && message.length() != 0) {
callback.onFailure(message);
} else {
Class exceptionClass = e.getClass();
message = exceptionClass.getName();
callback.onFailure(message);
}
}
}
}
@Override
public void onFailure(String message) {
if (callback != null) {
callback.onFailure(message);
}
}
}.toAsyncHandler());
}
});
}
public static interface Callback {
public void onSuccess(PolylineOptions polylineOptions);
public void onFailure(String message);
}
}