Added getters for PathOverlay points, refactored DirectionsOverlay

Change-Id: Ia46352151dad96ae30c427d7776614555913c48b
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-03-10 09:46:11 -05:00
parent 54de0d8060
commit 0c995e02d3
2 changed files with 138 additions and 120 deletions

View File

@@ -31,9 +31,16 @@ import com.google.android.maps.GeoPoint;
*/ */
public class DirectionsOverlay { public class DirectionsOverlay {
/**
* @author ricky barrette
*/
public interface OnDirectionsCompleteListener{
public void onDirectionsComplete(DirectionsOverlay directionsOverlay);
}
private static final String TAG = "DirectionsOverlay"; private static final String TAG = "DirectionsOverlay";
ArrayList<PathOverlay> mPath; private ArrayList<PathOverlay> mPath;
ArrayList<String> mDirections; private ArrayList<String> mDirections;
private MapView mMapView; private MapView mMapView;
private OnDirectionsCompleteListener mListener; private OnDirectionsCompleteListener mListener;
private String mCopyRights; private String mCopyRights;
@@ -71,6 +78,62 @@ public class DirectionsOverlay {
drawPath(json); drawPath(json);
} }
/**
* Deocodes googles polyline
* @param encoded
* @return a list of geopoints representing the path
* @author Mark McClure http://facstaff.unca.edu/mcmcclur/googlemaps/encodepolyline/
* @author ricky barrette
* @throws JSONException
*/
private void decodePoly(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;
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;
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;
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) 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;
}
}
/** /**
* Downloads Google Directions JSON from the Internet * Downloads Google Directions JSON from the Internet
* @param url * @param url
@@ -93,6 +156,7 @@ public class DirectionsOverlay {
return response.toString(); return response.toString();
} }
/** /**
* Creates a new DirectionsOverlay from the json provided * Creates a new DirectionsOverlay from the json provided
* @param json of Google Directions API * @param json of Google Directions API
@@ -193,59 +257,56 @@ public class DirectionsOverlay {
"&sensor=true&mode=walking"; "&sensor=true&mode=walking";
} }
/**
* @return
* @author ricky barrette
*/
public String getCopyrights(){
return mCopyRights;
}
/** /**
* Deocodes googles polyline * @return
* @param encoded
* @return a list of geopoints representing the path
* @author Mark McClure http://facstaff.unca.edu/mcmcclur/googlemaps/encodepolyline/
* @author ricky barrette * @author ricky barrette
* @throws JSONException
*/ */
private void decodePoly(JSONObject step) throws JSONException { public ArrayList<String> getDirections() {
if(Debug.DEBUG) return mDirections;
Log.d(TAG, "decodePoly"); }
String encoded = step.getJSONObject("polyline").getString("points"); /**
int index = 0, len = encoded.length(); * @param step
int lat = 0, lng = 0; * @return the distance of a step
* @throws JSONException
* @author ricky barrette
*/
private String getDistance(JSONObject step) throws JSONException{
return step.getJSONObject("distance").getString("text");
}
GeoPoint last = null; /**
while (index < len) { * @return
int b, shift = 0, result = 0; * @author ricky barrette
do { */
b = encoded.charAt(index++) - 63; public ArrayList<String> getDistances(){
result |= (b & 0x1f) << shift; return mDistance;
shift += 5; }
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0; /**
result = 0; * @param step
do { * @return the duration of a step
b = encoded.charAt(index++) - 63; * @throws JSONException
result |= (b & 0x1f) << shift; * @author ricky barrette
shift += 5; */
} while (b >= 0x20); private String getDuration(JSONObject step) throws JSONException{
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); return step.getJSONObject("duration").getString("text");
lng += dlng; }
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) 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));
last = p;
}
/**
* @return
* @author ricky barrette
*/
public ArrayList<String> getDurations(){
return mDuration;
} }
/** /**
@@ -259,26 +320,6 @@ public class DirectionsOverlay {
return new GeoPoint((int) (point.getDouble("lat")*1E6), (int) (point.getDouble("lng")*1E6)); return new GeoPoint((int) (point.getDouble("lat")*1E6), (int) (point.getDouble("lng")*1E6));
} }
/**
* @param step
* @return the duration of a step
* @throws JSONException
* @author ricky barrette
*/
private String getDuration(JSONObject step) throws JSONException{
return step.getJSONObject("duration").getString("text");
}
/**
* @param step
* @return the distance of a step
* @throws JSONException
* @author ricky barrette
*/
private String getDistance(JSONObject step) throws JSONException{
return step.getJSONObject("distance").getString("text");
}
/** /**
* @return the array of PathOverlays * @return the array of PathOverlays
* @author ricky barrette * @author ricky barrette
@@ -287,29 +328,6 @@ public class DirectionsOverlay {
return mPath; return mPath;
} }
/**
* Removes the directions overlay from the map view
* @author ricky barrette
*/
public void removePath() {
if(mMapView.getOverlays().removeAll(mPath));
}
/**
* @author ricky barrette
*/
public interface OnDirectionsCompleteListener{
public void onDirectionsComplete(DirectionsOverlay directionsOverlay);
}
/**
* @return
* @author ricky barrette
*/
public ArrayList<String> getDirections() {
return mDirections;
}
/** /**
* @return * @return
* @author ricky barrette * @author ricky barrette
@@ -318,30 +336,6 @@ public class DirectionsOverlay {
return mPoints; return mPoints;
} }
/**
* @return
* @author ricky barrette
*/
public ArrayList<String> getDurations(){
return mDuration;
}
/**
* @return
* @author ricky barrette
*/
public ArrayList<String> getDistances(){
return mDistance;
}
/**
* @return
* @author ricky barrette
*/
public String getCopyrights(){
return mCopyRights;
}
/** /**
* @return * @return
* @author ricky barrette * @author ricky barrette
@@ -349,4 +343,12 @@ public class DirectionsOverlay {
public ArrayList<String> getWarnings() { public ArrayList<String> getWarnings() {
return mWarnings; return mWarnings;
} }
/**
* Removes the directions overlay from the map view
* @author ricky barrette
*/
public void removePath() {
if(mMapView.getOverlays().removeAll(mPath));
}
} }

View File

@@ -53,7 +53,7 @@ public final class PathOverlay extends Overlay {
mMode = POINT; mMode = POINT;
mRadius = radius; mRadius = radius;
mStart = point; mStart = point;
mEnd = null; mEnd = mStart;
mColor = color; mColor = color;
} }
@@ -86,4 +86,20 @@ public final class PathOverlay extends Overlay {
} }
super.draw(canvas, mapView, shadow); super.draw(canvas, mapView, shadow);
} }
/**
* @return the end point of this path
* @author ricky barrette
*/
public GeoPoint getEndPoint(){
return this.mEnd;
}
/**
* @return the start point of this path
* @author ricky barrette
*/
public GeoPoint getStartPoint(){
return this.mStart;
}
} }