package com jtaxi services detectors import android content Context im

 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
package com.jtaxi.services.detectors;
import android.content.Context;
import android.location.Address;
import android.location.Location;
import com.google.android.gms.maps.model.LatLng;
import com.jtaxi.ProjectConfig;
import com.jtaxi.services.JsonClientUsage;
import com.jtaxi.services.JsonResponseHandler;
import com.jtaxi.services.http.RequestAction;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by Roman on 3/16/14.
*/
public class NearbyCarsDetector {
private static final int MAX_CARS = 8;
public static RequestAction getNearbyCars(Context context, LatLng latLng, final Callback callback) {
double latitude = latLng.latitude;
double longitude = latLng.longitude;
return getNearbyCars(context, latitude, longitude, callback);
}
public static RequestAction getNearbyCars(Context context, Location location, final Callback callback) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
return getNearbyCars(context, latitude, longitude, callback);
}
public static RequestAction getNearbyCars(Context context, Address address, final Callback callback) {
double latitude = address.getLatitude();
double longitude = address.getLongitude();
return getNearbyCars(context, latitude, longitude, callback);
}
public static RequestAction getNearbyCars(Context context, double latitude, double longitude, final Callback callback) {
return JsonClientUsage.getNearbyCars(context, latitude, longitude, new JsonResponseHandler.RequestCallback() {
@Override
public void onSuccess(JSONObject data) {
callback.onNotFound();
}
@Override
public void onSuccess(JSONArray data) {
ArrayList<JSONObject> nearbyCars = new ArrayList<JSONObject>(MAX_CARS);
for (int carIndex = 0; carIndex < data.length() && carIndex < MAX_CARS; carIndex++) {
try {
JSONObject car = data.getJSONObject(carIndex);
nearbyCars.add(car);
} catch (Exception e) {
if (ProjectConfig.DEBUG) {
e.printStackTrace();
}
}
}
if (nearbyCars.isEmpty()) {
callback.onNotFound();
} else {
callback.onFound(nearbyCars);
}
}
@Override
public void onFailure(String message) {
callback.onFailure(message);
}
});
}
public static interface Callback {
public void onFound(ArrayList<JSONObject> nearbyCars);
public void onNotFound();
public void onFailure(String message);
}
}