Updated Compat library and cleaned up code
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -30,14 +30,14 @@ import com.google.android.maps.GeoPoint;
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class DirectionsOverlay {
|
||||
|
||||
|
||||
/**
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public interface OnDirectionsCompleteListener{
|
||||
public void onDirectionsComplete(DirectionsOverlay directionsOverlay);
|
||||
}
|
||||
|
||||
|
||||
private static final String TAG = "DirectionsOverlay";
|
||||
private ArrayList<PathOverlay> mPath;
|
||||
private ArrayList<String> mDirections;
|
||||
@@ -48,24 +48,24 @@ public class DirectionsOverlay {
|
||||
private ArrayList<String> mDistance;
|
||||
private ArrayList<String> mDuration;
|
||||
private ArrayList<String> mWarnings;
|
||||
|
||||
|
||||
/**
|
||||
* Downloads and Creates a new DirectionsOverlay from the provided points
|
||||
* @param origin point
|
||||
* @param destination point
|
||||
* @author ricky barrette
|
||||
* @throws IOException
|
||||
* @throws ClientProtocolException
|
||||
* @throws IllegalStateException
|
||||
* @throws JSONException
|
||||
* @throws IOException
|
||||
* @throws ClientProtocolException
|
||||
* @throws IllegalStateException
|
||||
* @throws JSONException
|
||||
*/
|
||||
public DirectionsOverlay(final MapView map, final GeoPoint origin, final GeoPoint destination, final OnDirectionsCompleteListener listener) throws IllegalStateException, ClientProtocolException, IOException, JSONException {
|
||||
mMapView = map;
|
||||
mListener = listener;
|
||||
String json = downloadJSON(generateUrl(origin, destination));
|
||||
final String json = downloadJSON(generateUrl(origin, destination));
|
||||
drawPath(json);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new DirectionsOverlay from the provided String JSON
|
||||
* @param json
|
||||
@@ -77,65 +77,66 @@ public class DirectionsOverlay {
|
||||
mMapView = map;
|
||||
drawPath(json);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deocodes googles polyline
|
||||
* @param encoded
|
||||
* @return a list of geopoints representing the path
|
||||
* @return a list of geopoints representing the path
|
||||
* @author Mark McClure http://facstaff.unca.edu/mcmcclur/googlemaps/encodepolyline/
|
||||
* @author ricky barrette
|
||||
* @throws JSONException
|
||||
* @throws JSONException
|
||||
*/
|
||||
private void decodePoly(final JSONObject step) throws JSONException {
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "decodePoly");
|
||||
|
||||
String encoded = step.getJSONObject("polyline").getString("points");
|
||||
int index = 0, len = encoded.length();
|
||||
int lat = 0, lng = 0;
|
||||
final String encoded = step.getJSONObject("polyline").getString("points");
|
||||
int index = 0;
|
||||
final int len = encoded.length();
|
||||
int lat = 0, lng = 0;
|
||||
|
||||
GeoPoint last = null;
|
||||
while (index < len) {
|
||||
int b, shift = 0, result = 0;
|
||||
do {
|
||||
b = encoded.charAt(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20);
|
||||
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
||||
lat += dlat;
|
||||
GeoPoint last = null;
|
||||
while (index < len) {
|
||||
int b, shift = 0, result = 0;
|
||||
do {
|
||||
b = encoded.charAt(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20);
|
||||
final int dlat = (result & 1) != 0 ? ~(result >> 1) : result >> 1;
|
||||
lat += dlat;
|
||||
|
||||
shift = 0;
|
||||
result = 0;
|
||||
do {
|
||||
b = encoded.charAt(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20);
|
||||
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
||||
lng += dlng;
|
||||
shift = 0;
|
||||
result = 0;
|
||||
do {
|
||||
b = encoded.charAt(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20);
|
||||
final int dlng = (result & 1) != 0 ? ~(result >> 1) : result >> 1;
|
||||
lng += dlng;
|
||||
|
||||
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
|
||||
|
||||
if(Debug.DEBUG){
|
||||
final GeoPoint p = new GeoPoint((int) (lat / 1E5 * 1E6), (int) (lng / 1E5 * 1E6));
|
||||
|
||||
if(Debug.DEBUG){
|
||||
Log.d(TAG, "current = "+ p.toString());
|
||||
if(last != null)
|
||||
Log.d(TAG, "last = "+ last.toString());
|
||||
}
|
||||
|
||||
|
||||
if(last != null)
|
||||
mPath.add(new PathOverlay(last, p, Color.RED));
|
||||
// else
|
||||
// mPath.add(new PathOverlay(p, 5, Color.GREEN));
|
||||
|
||||
last = p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if(last != null)
|
||||
mPath.add(new PathOverlay(last, p, Color.RED));
|
||||
// else
|
||||
// mPath.add(new PathOverlay(p, 5, Color.GREEN));
|
||||
|
||||
last = p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Downloads Google Directions JSON from the Internet
|
||||
* Downloads Google Directions JSON from the Internet
|
||||
* @param url
|
||||
* @return
|
||||
* @throws IllegalStateException
|
||||
@@ -148,21 +149,21 @@ public class DirectionsOverlay {
|
||||
Log.d(TAG, url);
|
||||
if(url == null)
|
||||
throw new NullPointerException();
|
||||
StringBuffer response = new StringBuffer();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(new DefaultHttpClient().execute(new HttpGet(url)).getEntity().getContent()));
|
||||
final StringBuffer response = new StringBuffer();
|
||||
final BufferedReader br = new BufferedReader(new InputStreamReader(new DefaultHttpClient().execute(new HttpGet(url)).getEntity().getContent()));
|
||||
String buff = null;
|
||||
while ((buff = br.readLine()) != null)
|
||||
response.append(buff);
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new DirectionsOverlay from the json provided
|
||||
* @param json of Google Directions API
|
||||
* @author ricky barrette
|
||||
* @return
|
||||
* @throws JSONException
|
||||
* @return
|
||||
* @throws JSONException
|
||||
*/
|
||||
public void drawPath(final String json) throws JSONException{
|
||||
if(Debug.DEBUG){
|
||||
@@ -174,75 +175,75 @@ public class DirectionsOverlay {
|
||||
mPoints = new ArrayList<GeoPoint>();
|
||||
mDistance = new ArrayList<String>();
|
||||
mDuration = new ArrayList<String>();
|
||||
|
||||
|
||||
//get first route
|
||||
JSONObject route = new JSONObject(json).getJSONArray("routes").getJSONObject(0);
|
||||
|
||||
final JSONObject route = new JSONObject(json).getJSONArray("routes").getJSONObject(0);
|
||||
|
||||
mCopyRights = route.getString("copyrights");
|
||||
//route.getString("status");
|
||||
|
||||
JSONObject leg = route.getJSONArray("legs").getJSONObject(0);
|
||||
|
||||
final JSONObject leg = route.getJSONArray("legs").getJSONObject(0);
|
||||
getDistance(leg);
|
||||
getDuration(leg);
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(leg.getJSONObject("start_location")), 12, Color.GREEN));
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(leg.getJSONObject("end_location")), 12, Color.RED));
|
||||
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(leg.getJSONObject("start_location")), 12, Color.GREEN));
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(leg.getJSONObject("end_location")), 12, Color.RED));
|
||||
|
||||
leg.getString("start_address");
|
||||
leg.getString("end_address");
|
||||
|
||||
// JSONArray warnings = leg.getJSONArray("warnings");
|
||||
// for(int i = 0; i < warnings.length(); i++){
|
||||
// mWarnings.add(warnings.get)w
|
||||
// }w
|
||||
|
||||
|
||||
// JSONArray warnings = leg.getJSONArray("warnings");
|
||||
// for(int i = 0; i < warnings.length(); i++){
|
||||
// mWarnings.add(warnings.get)w
|
||||
// }w
|
||||
|
||||
/*
|
||||
* here we will parse the steps of the directions
|
||||
*/
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "processing steps");
|
||||
JSONArray steps = leg.getJSONArray("steps");
|
||||
final JSONArray steps = leg.getJSONArray("steps");
|
||||
JSONObject step = null;
|
||||
for(int i = 0; i < steps.length(); i++){
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "step "+i);
|
||||
|
||||
|
||||
step = steps.getJSONObject(i);
|
||||
|
||||
|
||||
if(Debug.DEBUG){
|
||||
Log.d(TAG, "start "+getGeoPoint(step.getJSONObject("start_location")).toString());
|
||||
Log.d(TAG, "end "+getGeoPoint(step.getJSONObject("end_location")).toString());
|
||||
}
|
||||
|
||||
// if(Debug.DEBUG)
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(step.getJSONObject("start_location")), getGeoPoint(step.getJSONObject("end_location")), Color.MAGENTA));
|
||||
|
||||
|
||||
// if(Debug.DEBUG)
|
||||
// mMapView.getOverlays().add(new PathOverlay(getGeoPoint(step.getJSONObject("start_location")), getGeoPoint(step.getJSONObject("end_location")), Color.MAGENTA));
|
||||
|
||||
decodePoly(step);
|
||||
|
||||
mDuration.add(getDuration(step));
|
||||
|
||||
|
||||
mDistance.add(getDistance(step));
|
||||
|
||||
|
||||
mDirections.add(step.getString("html_instructions"));
|
||||
// Log.d("TEST", step.getString("html_instructions"));
|
||||
// Log.d("TEST", step.getString("html_instructions"));
|
||||
mPoints.add(getGeoPoint(step.getJSONObject("start_location")));
|
||||
|
||||
|
||||
}
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, "finished parsing");
|
||||
|
||||
|
||||
if(mMapView != null){
|
||||
mMapView.getOverlays().addAll(mPath);
|
||||
mMapView.postInvalidate();
|
||||
}
|
||||
|
||||
|
||||
if(mListener != null)
|
||||
mListener.onDirectionsComplete(DirectionsOverlay.this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param origin
|
||||
* @param destination
|
||||
* @return The Google API url for our directions
|
||||
* @return The Google API url for our directions
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private String generateUrl(final GeoPoint origin, final GeoPoint destination){
|
||||
@@ -256,7 +257,7 @@ public class DirectionsOverlay {
|
||||
Double.toString(destination.getLongitudeE6() / 1.0E6)+
|
||||
"&sensor=true&mode=walking";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
@@ -264,7 +265,7 @@ public class DirectionsOverlay {
|
||||
public String getCopyrights(){
|
||||
return mCopyRights;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
@@ -282,7 +283,7 @@ public class DirectionsOverlay {
|
||||
private String getDistance(final JSONObject step) throws JSONException{
|
||||
return step.getJSONObject("distance").getString("text");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
@@ -293,7 +294,7 @@ public class DirectionsOverlay {
|
||||
|
||||
/**
|
||||
* @param step
|
||||
* @return the duration of a step
|
||||
* @return the duration of a step
|
||||
* @throws JSONException
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@@ -327,7 +328,7 @@ public class DirectionsOverlay {
|
||||
public ArrayList<PathOverlay> getPath(){
|
||||
return mPath;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
@@ -349,6 +350,6 @@ public class DirectionsOverlay {
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void removePath() {
|
||||
if(mMapView.getOverlays().removeAll(mPath));
|
||||
if(mMapView.getOverlays().removeAll(mPath));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user