Updated Compat library and cleaned up code
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -33,13 +33,13 @@ import com.google.android.maps.Projection;
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public abstract class BaseUserOverlay extends Overlay implements GeoPointLocationListener, CompassListener {
|
||||
|
||||
|
||||
/**
|
||||
* This thread is responsible for animating the user icon
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class AnimationThread extends Thread {
|
||||
|
||||
|
||||
private boolean isAborted;
|
||||
|
||||
public void abort(){
|
||||
@@ -51,59 +51,57 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Thread#run()
|
||||
*/
|
||||
@Override
|
||||
@Override
|
||||
public void run(){
|
||||
super.run();
|
||||
int index = 0;
|
||||
boolean isCountingDown = false;
|
||||
while (true) {
|
||||
while (true)
|
||||
synchronized (this) {
|
||||
if (isAborted) {
|
||||
if (isAborted)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
switch(index){
|
||||
case 1:
|
||||
mUserArrow = R.drawable.user_arrow_animation_2;
|
||||
if(isCountingDown)
|
||||
case 1:
|
||||
mUserArrow = R.drawable.user_arrow_animation_2;
|
||||
if(isCountingDown)
|
||||
index--;
|
||||
else
|
||||
index++;
|
||||
|
||||
try {
|
||||
sleep(100l);
|
||||
} catch (final InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
mUserArrow = R.drawable.user_arrow_animation_3;
|
||||
index--;
|
||||
else
|
||||
isCountingDown = true;
|
||||
try {
|
||||
sleep(200l);
|
||||
} catch (final InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
mUserArrow = R.drawable.user_arrow_animation_1;
|
||||
index++;
|
||||
|
||||
try {
|
||||
sleep(100l);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
mUserArrow = R.drawable.user_arrow_animation_3;
|
||||
index--;
|
||||
isCountingDown = true;
|
||||
try {
|
||||
sleep(200l);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
mUserArrow = R.drawable.user_arrow_animation_1;
|
||||
index++;
|
||||
isCountingDown = false;
|
||||
try {
|
||||
sleep(2000l);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
isCountingDown = false;
|
||||
try {
|
||||
sleep(2000l);
|
||||
} catch (final InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final String TAG = "UserOverlayBase";
|
||||
private boolean isEnabled;
|
||||
private int mUserArrow = R.drawable.user_arrow_animation_1;
|
||||
@@ -111,30 +109,30 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
private float mBearing = 0;
|
||||
private int mAccuracy;
|
||||
private GeoPoint mPoint;
|
||||
private Context mContext;
|
||||
private MapView mMapView;
|
||||
private final Context mContext;
|
||||
private final MapView mMapView;
|
||||
private boolean isFistFix = true;
|
||||
private GeoPointLocationListener mListener;
|
||||
public boolean isFollowingUser = true;
|
||||
private CompasOverlay mCompass;
|
||||
private final CompasOverlay mCompass;
|
||||
private boolean isCompassEnabled;
|
||||
|
||||
|
||||
private CompassListener mCompassListener;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new UserOverlay
|
||||
* @param mapView
|
||||
* @param context
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public BaseUserOverlay(MapView mapView, Context context) {
|
||||
public BaseUserOverlay(final MapView mapView, final Context context) {
|
||||
super();
|
||||
mContext = context;
|
||||
mMapView = mapView;
|
||||
mCompass = new CompasOverlay(context);
|
||||
mUserArrow = R.drawable.user_arrow_animation_1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new UserOverlayTODO Auto-generated method stub
|
||||
* @param mapView
|
||||
@@ -142,11 +140,11 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* @param followUser
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public BaseUserOverlay(MapView mapView, Context context, boolean followUser) {
|
||||
public BaseUserOverlay(final MapView mapView, final Context context, final boolean followUser) {
|
||||
this(mapView, context);
|
||||
isFollowingUser = followUser;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disables the compass
|
||||
* @author ricky barrette
|
||||
@@ -155,7 +153,7 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
isCompassEnabled = false;
|
||||
mMapView.getOverlays().remove(mCompass);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stops location updates and removes the overlay from view
|
||||
* @author ricky barrette
|
||||
@@ -169,7 +167,7 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
mListener.onFirstFix(false);
|
||||
mAnimationThread.abort();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* we override this methods so we can provide a drawable and a location to draw on the canvas.
|
||||
* (non-Javadoc)
|
||||
@@ -180,12 +178,12 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public void draw(Canvas canvas, MapView mapView, boolean shadow){
|
||||
public void draw(Canvas canvas, final MapView mapView, final boolean shadow){
|
||||
if (isEnabled && mPoint != null) {
|
||||
Point center = new Point();
|
||||
Point left = new Point();
|
||||
Projection projection = mapView.getProjection();
|
||||
GeoPoint leftGeo = GeoUtils.distanceFrom(mPoint, mAccuracy);
|
||||
final Point center = new Point();
|
||||
final Point left = new Point();
|
||||
final Projection projection = mapView.getProjection();
|
||||
final GeoPoint leftGeo = GeoUtils.distanceFrom(mPoint, mAccuracy);
|
||||
projection.toPixels(leftGeo, left);
|
||||
projection.toPixels(mPoint, center);
|
||||
canvas = drawAccuracyCircle(center, left, canvas);
|
||||
@@ -194,7 +192,7 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* the following log is used to demonstrate if the leftGeo point is the correct
|
||||
*/
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG, (GeoUtils.distanceKm(mPoint, leftGeo) * 1000)+"m");
|
||||
Log.d(TAG, GeoUtils.distanceKm(mPoint, leftGeo) * 1000+"m");
|
||||
}
|
||||
super.draw(canvas, mapView, shadow);
|
||||
}
|
||||
@@ -207,96 +205,95 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* @return modified canvas
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private Canvas drawAccuracyCircle(Point center, Point left, Canvas canvas) {
|
||||
Paint paint = new Paint();
|
||||
|
||||
/*
|
||||
* get radius of the circle being drawn by
|
||||
*/
|
||||
int circleRadius = center.x - left.x;
|
||||
if(circleRadius <= 0){
|
||||
circleRadius = left.x - center.x;
|
||||
}
|
||||
/*
|
||||
* paint a blue circle on the map
|
||||
*/
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStrokeWidth(2.0f);
|
||||
paint.setColor(Color.BLUE);
|
||||
paint.setStyle(Style.STROKE);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
private Canvas drawAccuracyCircle(final Point center, final Point left, final Canvas canvas) {
|
||||
final Paint paint = new Paint();
|
||||
|
||||
/*
|
||||
* get radius of the circle being drawn by
|
||||
*/
|
||||
int circleRadius = center.x - left.x;
|
||||
if(circleRadius <= 0)
|
||||
circleRadius = left.x - center.x;
|
||||
/*
|
||||
* paint a blue circle on the map
|
||||
*/
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStrokeWidth(2.0f);
|
||||
paint.setColor(Color.BLUE);
|
||||
paint.setStyle(Style.STROKE);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
/*
|
||||
* fill the radius with a alpha blue
|
||||
*/
|
||||
paint.setAlpha(30);
|
||||
paint.setStyle(Style.FILL);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
|
||||
/*
|
||||
* for testing
|
||||
* draw a dot over the left geopoint
|
||||
*/
|
||||
if(Debug.DEBUG){
|
||||
paint.setColor(Color.RED);
|
||||
RectF oval = new RectF(left.x - 1, left.y - 1, left.x + 1, left.y + 1);
|
||||
paint.setStyle(Style.FILL);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
|
||||
/*
|
||||
* for testing
|
||||
* draw a dot over the left geopoint
|
||||
*/
|
||||
if(Debug.DEBUG){
|
||||
paint.setColor(Color.RED);
|
||||
final RectF oval = new RectF(left.x - 1, left.y - 1, left.x + 1, left.y + 1);
|
||||
canvas.drawOval(oval, paint);
|
||||
}
|
||||
|
||||
return canvas;
|
||||
}
|
||||
|
||||
return canvas;
|
||||
}
|
||||
|
||||
/**
|
||||
* draws user arrow that points north based on bearing onto the supplied canvas
|
||||
* @param point to draw user arrow on
|
||||
* @param bearing of the device
|
||||
* @param canvas to draw on
|
||||
* @return modified canvas
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private Canvas drawUser(Point point, float bearing, Canvas canvas){
|
||||
Bitmap user = BitmapFactory.decodeResource(mContext.getResources(), mUserArrow);
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postRotate(bearing);
|
||||
Bitmap rotatedBmp = Bitmap.createBitmap(
|
||||
user,
|
||||
0, 0,
|
||||
user.getWidth(),
|
||||
user.getHeight(),
|
||||
matrix,
|
||||
true
|
||||
);
|
||||
canvas.drawBitmap(
|
||||
rotatedBmp,
|
||||
point.x - (rotatedBmp.getWidth() / 2),
|
||||
point.y - (rotatedBmp.getHeight() / 2),
|
||||
null
|
||||
);
|
||||
return canvas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the compass
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void enableCompass(){
|
||||
if(! this.isCompassEnabled){
|
||||
this.mMapView.getOverlays().add(this.mCompass);
|
||||
this.isCompassEnabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to enable MyLocation, registering for updates from provider
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void enableMyLocation(){
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG,"enableMyLocation()");
|
||||
if (! isEnabled) {
|
||||
|
||||
mAnimationThread = new AnimationThread();
|
||||
mAnimationThread.start();
|
||||
|
||||
|
||||
/**
|
||||
* draws user arrow that points north based on bearing onto the supplied canvas
|
||||
* @param point to draw user arrow on
|
||||
* @param bearing of the device
|
||||
* @param canvas to draw on
|
||||
* @return modified canvas
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private Canvas drawUser(final Point point, final float bearing, final Canvas canvas){
|
||||
final Bitmap user = BitmapFactory.decodeResource(mContext.getResources(), mUserArrow);
|
||||
final Matrix matrix = new Matrix();
|
||||
matrix.postRotate(bearing);
|
||||
final Bitmap rotatedBmp = Bitmap.createBitmap(
|
||||
user,
|
||||
0, 0,
|
||||
user.getWidth(),
|
||||
user.getHeight(),
|
||||
matrix,
|
||||
true
|
||||
);
|
||||
canvas.drawBitmap(
|
||||
rotatedBmp,
|
||||
point.x - rotatedBmp.getWidth() / 2,
|
||||
point.y - rotatedBmp.getHeight() / 2,
|
||||
null
|
||||
);
|
||||
return canvas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the compass
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void enableCompass(){
|
||||
if(! isCompassEnabled){
|
||||
mMapView.getOverlays().add(mCompass);
|
||||
isCompassEnabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to enable MyLocation, registering for updates from provider
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void enableMyLocation(){
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG,"enableMyLocation()");
|
||||
if (! isEnabled) {
|
||||
|
||||
mAnimationThread = new AnimationThread();
|
||||
mAnimationThread.start();
|
||||
|
||||
onMyLocationEnabled();
|
||||
isEnabled = true;
|
||||
mCompass.enable(this);
|
||||
@@ -304,37 +301,37 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
if(mListener != null)
|
||||
mListener.onFirstFix(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the map to follow the user
|
||||
* @param followUser
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void followUser(boolean followUser){
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG,"followUser()");
|
||||
isFollowingUser = followUser;
|
||||
}
|
||||
|
||||
/**
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the map to follow the user
|
||||
* @param followUser
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void followUser(final boolean followUser){
|
||||
if(Debug.DEBUG)
|
||||
Log.d(TAG,"followUser()");
|
||||
isFollowingUser = followUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return return the current destination
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public GeoPoint getDestination(){
|
||||
return mCompass.getDestination();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the users current bearing
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public float getUserBearing(){
|
||||
return mBearing;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* returns the users current bearing
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public float getUserBearing(){
|
||||
return mBearing;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the users current location
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
@@ -342,9 +339,9 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
public GeoPoint getUserLocation(){
|
||||
return mPoint;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCompassUpdate(float bearing) {
|
||||
public void onCompassUpdate(final float bearing) {
|
||||
if(mCompassListener != null)
|
||||
mCompassListener.onCompassUpdate(bearing);
|
||||
mBearing = bearing;
|
||||
@@ -360,34 +357,32 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public void onLocationChanged(GeoPoint point, int accuracy) {
|
||||
|
||||
public void onLocationChanged(final GeoPoint point, final int accuracy) {
|
||||
|
||||
if(mCompass != null)
|
||||
mCompass.setLocation(point);
|
||||
|
||||
|
||||
/*
|
||||
* if this is the first fix
|
||||
* set map center the users location, and zoom to the max zoom level
|
||||
*/
|
||||
if(point != null && isFistFix){
|
||||
mMapView.getController().setCenter(point);
|
||||
mMapView.getController().setZoom( (mMapView.getMaxZoomLevel() - 2) );
|
||||
mMapView.getController().setZoom( mMapView.getMaxZoomLevel() - 2 );
|
||||
if(mListener != null)
|
||||
mListener.onFirstFix(true);
|
||||
isFistFix = false;
|
||||
}
|
||||
|
||||
|
||||
//update the users point, and accuracy for the UI
|
||||
mPoint = point;
|
||||
mAccuracy = accuracy;
|
||||
mMapView.invalidate();
|
||||
if(mListener != null){
|
||||
if(mListener != null)
|
||||
mListener.onLocationChanged(point, accuracy);
|
||||
}
|
||||
|
||||
if (isFollowingUser) {
|
||||
|
||||
if (isFollowingUser)
|
||||
panToUserIfOffMap(point);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -397,43 +392,41 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
public abstract void onMyLocationDisabled();
|
||||
|
||||
/**
|
||||
* Called when the enableMyLocation() is called. This is where you want to ask your location provider for updates
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public abstract void onMyLocationEnabled();
|
||||
|
||||
* Called when the enableMyLocation() is called. This is where you want to ask your location provider for updates
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public abstract void onMyLocationEnabled();
|
||||
|
||||
/**
|
||||
* pans the map view if the user is off screen.
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private void panToUserIfOffMap(GeoPoint user) {
|
||||
GeoPoint center = mMapView.getMapCenter();
|
||||
double distance = GeoUtils.distanceKm(center, user);
|
||||
double distanceLat = GeoUtils.distanceKm(center, new GeoPoint((center.getLatitudeE6() + (int) (mMapView.getLatitudeSpan() / 2)), center.getLongitudeE6()));
|
||||
double distanceLon = GeoUtils.distanceKm(center, new GeoPoint(center.getLatitudeE6(), (center.getLongitudeE6() + (int) (mMapView.getLongitudeSpan() / 2))));
|
||||
|
||||
double whichIsGreater = (distanceLat > distanceLon) ? distanceLat : distanceLon;
|
||||
|
||||
private void panToUserIfOffMap(final GeoPoint user) {
|
||||
final GeoPoint center = mMapView.getMapCenter();
|
||||
final double distance = GeoUtils.distanceKm(center, user);
|
||||
final double distanceLat = GeoUtils.distanceKm(center, new GeoPoint(center.getLatitudeE6() + mMapView.getLatitudeSpan() / 2, center.getLongitudeE6()));
|
||||
final double distanceLon = GeoUtils.distanceKm(center, new GeoPoint(center.getLatitudeE6(), center.getLongitudeE6() + mMapView.getLongitudeSpan() / 2));
|
||||
|
||||
final double whichIsGreater = distanceLat > distanceLon ? distanceLat : distanceLon;
|
||||
|
||||
/**
|
||||
* if the user is one the map, keep them their
|
||||
* else don't pan to user unless they pan pack to them
|
||||
*/
|
||||
if( ! (distance > whichIsGreater) )
|
||||
if (distance > distanceLat || distance > distanceLon){
|
||||
if (distance > distanceLat || distance > distanceLon)
|
||||
mMapView.getController().animateTo(user);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempts to register the listener for location updates
|
||||
* @param listener
|
||||
* @author Ricky Barrette
|
||||
*/
|
||||
public void registerListener(GeoPointLocationListener listener){
|
||||
public void registerListener(final GeoPointLocationListener listener){
|
||||
Log.d(TAG,"registerListener()");
|
||||
if (mListener == null){
|
||||
if (mListener == null)
|
||||
mListener = listener;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -444,7 +437,7 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* @param y
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setCompassDrawables(int needleResId, int backgroundResId, int x, int y) {
|
||||
public void setCompassDrawables(final int needleResId, final int backgroundResId, final int x, final int y) {
|
||||
mCompass.setDrawables(needleResId, backgroundResId, x, y);
|
||||
}
|
||||
|
||||
@@ -453,19 +446,19 @@ public abstract class BaseUserOverlay extends Overlay implements GeoPointLocatio
|
||||
* @param listener
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setCompassListener(CompassListener listener){
|
||||
public void setCompassListener(final CompassListener listener){
|
||||
mCompassListener = listener;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the destination for the compass
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setDestination(GeoPoint destination){
|
||||
public void setDestination(final GeoPoint destination){
|
||||
if(mCompass != null)
|
||||
mCompass.setDestination(destination);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* UnResgisters the listener. after this call you will no longer get location updates
|
||||
* @author Ricky Barrette
|
||||
|
||||
@@ -45,20 +45,20 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
* Creates a new CompasOverlay
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public CompasOverlay(Context context) {
|
||||
public CompasOverlay(final Context context) {
|
||||
mContext = context;
|
||||
mCompassSensor = new CompassSensor(context);
|
||||
mX = convertDipToPx(40);
|
||||
mY = mX;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new CompasOverlay
|
||||
* @param context
|
||||
* @param destination
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public CompasOverlay(Context context, GeoPoint destination){
|
||||
public CompasOverlay(final Context context, final GeoPoint destination){
|
||||
this(context);
|
||||
mDestination = destination;
|
||||
}
|
||||
@@ -73,14 +73,14 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
* @param y dip
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public CompasOverlay(Context context, GeoPoint destination, int needleResId, int backgroundResId, int x, int y){
|
||||
public CompasOverlay(final Context context, final GeoPoint destination, final int needleResId, final int backgroundResId, final int x, final int y){
|
||||
this(context, destination);
|
||||
mX = convertDipToPx(x);
|
||||
mY = convertDipToPx(y);
|
||||
mNeedleResId = needleResId;
|
||||
mBackgroundResId = backgroundResId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new CompasOverlay
|
||||
* @param context
|
||||
@@ -90,7 +90,7 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
* @param y
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public CompasOverlay(Context context, int needleResId, int backgroundResId, int x, int y){
|
||||
public CompasOverlay(final Context context, final int needleResId, final int backgroundResId, final int x, final int y){
|
||||
this(context, null, needleResId, backgroundResId, x, y);
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
* @return px
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private int convertDipToPx(int i) {
|
||||
Resources r = mContext.getResources();
|
||||
private int convertDipToPx(final int i) {
|
||||
final Resources r = mContext.getResources();
|
||||
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i, r.getDisplayMetrics());
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
mCompassSensor.disable();
|
||||
mListener = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* (non-Javadoc)
|
||||
* @see com.google.android.maps.Overlay#draw(android.graphics.Canvas, com.google.android.maps.MapView, boolean)
|
||||
@@ -122,43 +122,43 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
*/
|
||||
@Override
|
||||
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
|
||||
|
||||
|
||||
if(isEnabled){
|
||||
//set the center of the compass in the top left corner of the screen
|
||||
Point point = new Point();
|
||||
final Point point = new Point();
|
||||
point.set(mX, mY);
|
||||
|
||||
|
||||
//draw compass background
|
||||
Bitmap compass = BitmapFactory.decodeResource( mContext.getResources(), mBackgroundResId);
|
||||
canvas.drawBitmap(compass,
|
||||
point.x - (compass.getWidth() / 2),
|
||||
point.y - (compass.getHeight() / 2),
|
||||
null
|
||||
);
|
||||
|
||||
final Bitmap compass = BitmapFactory.decodeResource( mContext.getResources(), mBackgroundResId);
|
||||
canvas.drawBitmap(compass,
|
||||
point.x - compass.getWidth() / 2,
|
||||
point.y - compass.getHeight() / 2,
|
||||
null
|
||||
);
|
||||
|
||||
//draw the compass needle
|
||||
Bitmap arrowBitmap = BitmapFactory.decodeResource( mContext.getResources(), mNeedleResId);
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postRotate(GeoUtils.calculateBearing(mLocation, mDestination, mBearing));
|
||||
Bitmap rotatedBmp = Bitmap.createBitmap(
|
||||
arrowBitmap,
|
||||
0, 0,
|
||||
arrowBitmap.getWidth(),
|
||||
arrowBitmap.getHeight(),
|
||||
matrix,
|
||||
true
|
||||
);
|
||||
final Bitmap arrowBitmap = BitmapFactory.decodeResource( mContext.getResources(), mNeedleResId);
|
||||
final Matrix matrix = new Matrix();
|
||||
matrix.postRotate(GeoUtils.calculateBearing(mLocation, mDestination, mBearing));
|
||||
final Bitmap rotatedBmp = Bitmap.createBitmap(
|
||||
arrowBitmap,
|
||||
0, 0,
|
||||
arrowBitmap.getWidth(),
|
||||
arrowBitmap.getHeight(),
|
||||
matrix,
|
||||
true
|
||||
);
|
||||
canvas.drawBitmap(
|
||||
rotatedBmp,
|
||||
point.x - (rotatedBmp.getWidth() / 2),
|
||||
point.y - (rotatedBmp.getHeight() / 2),
|
||||
null
|
||||
);
|
||||
rotatedBmp,
|
||||
point.x - rotatedBmp.getWidth() / 2,
|
||||
point.y - rotatedBmp.getHeight() / 2,
|
||||
null
|
||||
);
|
||||
mapView.invalidate();
|
||||
}
|
||||
super.draw(canvas, mapView, shadow);
|
||||
super.draw(canvas, mapView, shadow);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enables the compass overlay
|
||||
* @author ricky barrette
|
||||
@@ -169,17 +169,17 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
mCompassSensor.enable(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enables the compass overlay
|
||||
* @param listener
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void enable(CompassListener listener){
|
||||
public void enable(final CompassListener listener){
|
||||
mListener = listener;
|
||||
enable();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the current bearing
|
||||
* @author ricky barrette
|
||||
@@ -187,7 +187,7 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
public float getBearing(){
|
||||
return mBearing;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return return the current destination
|
||||
* @author ricky barrette
|
||||
@@ -203,24 +203,24 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@Override
|
||||
public void onCompassUpdate(float bearing) {
|
||||
public void onCompassUpdate(final float bearing) {
|
||||
mBearing = bearing;
|
||||
|
||||
|
||||
/*
|
||||
* pass it down the chain
|
||||
*/
|
||||
if(mListener != null)
|
||||
mListener.onCompassUpdate(bearing);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param destination
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setDestination(GeoPoint destination){
|
||||
public void setDestination(final GeoPoint destination){
|
||||
mDestination = destination;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param needleResId
|
||||
* @param backgroundResId
|
||||
@@ -228,18 +228,18 @@ public class CompasOverlay extends Overlay implements CompassListener {
|
||||
* @param y dip
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setDrawables(int needleResId, int backgroundResId, int x, int y){
|
||||
public void setDrawables(final int needleResId, final int backgroundResId, final int x, final int y){
|
||||
mX = convertDipToPx(x);
|
||||
mY = convertDipToPx(y);
|
||||
mNeedleResId = needleResId;
|
||||
mBackgroundResId = backgroundResId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param location
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setLocation(GeoPoint location){
|
||||
public void setLocation(final GeoPoint location){
|
||||
mLocation = location;
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public final class PathOverlay extends Overlay {
|
||||
mMode = PATH;
|
||||
mRadius = 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new PathOverlay in point mode. This is used to draw end points.
|
||||
* @param point
|
||||
@@ -56,29 +56,29 @@ public final class PathOverlay extends Overlay {
|
||||
mEnd = mStart;
|
||||
mColor = color;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param canvas canvas to be drawn on
|
||||
* @param mapView
|
||||
* @param shadow
|
||||
* @param mapView
|
||||
* @param shadow
|
||||
* @param when
|
||||
*/
|
||||
@Override
|
||||
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
|
||||
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
|
||||
final Projection projection = mapView.getProjection();
|
||||
final Paint paint = new Paint();
|
||||
paint.setColor(mColor);
|
||||
paint.setAntiAlias(true);
|
||||
Point point = new Point();
|
||||
final Point point = new Point();
|
||||
projection.toPixels(mStart, point);
|
||||
|
||||
|
||||
switch (mMode){
|
||||
case POINT:
|
||||
RectF oval = new RectF(point.x - mRadius, point.y - mRadius, point.x + mRadius, point.y + mRadius);
|
||||
final RectF oval = new RectF(point.x - mRadius, point.y - mRadius, point.x + mRadius, point.y + mRadius);
|
||||
canvas.drawOval(oval, paint);
|
||||
case PATH:
|
||||
Point point2 = new Point();
|
||||
final Point point2 = new Point();
|
||||
projection.toPixels(mEnd, point2);
|
||||
paint.setStrokeWidth(5);
|
||||
paint.setAlpha(120);
|
||||
@@ -86,20 +86,20 @@ public final class PathOverlay extends Overlay {
|
||||
}
|
||||
super.draw(canvas, mapView, shadow);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the end point of this path
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public GeoPoint getEndPoint(){
|
||||
return this.mEnd;
|
||||
return mEnd;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the start point of this path
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public GeoPoint getStartPoint(){
|
||||
return this.mStart;
|
||||
return mStart;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @author Twenty Codes
|
||||
* @author ricky barrette
|
||||
*/
|
||||
* @author Twenty Codes
|
||||
* @author ricky barrette
|
||||
*/
|
||||
|
||||
package com.TwentyCodes.android.overlays;
|
||||
|
||||
@@ -21,7 +21,7 @@ import com.google.android.maps.OverlayItem;
|
||||
import com.google.android.maps.Projection;
|
||||
|
||||
/**
|
||||
* This class will used to draw a radius of a specified size in a specified location, then inserted into
|
||||
* This class will used to draw a radius of a specified size in a specified location, then inserted into
|
||||
* an overlay list to be displayed a map
|
||||
* @author ricky barrette
|
||||
*/
|
||||
@@ -33,32 +33,32 @@ public class RadiusOverlay extends Overlay{
|
||||
private int mColor = Color.GREEN;
|
||||
private GeoPoint mRadiusPoint;
|
||||
private OnLocationSelectedListener mListener;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new RadiusOverlay
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public RadiusOverlay(){
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new RadiusOverlay object that can be inserted into an overlay list.
|
||||
* @param point center of radius geopoint
|
||||
* @param point center of radius geopoint
|
||||
* @param radius radius in meters
|
||||
* @param color desired color of the radius from Color API
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public RadiusOverlay(GeoPoint point, float radius, int color) {
|
||||
public RadiusOverlay(final GeoPoint point, final float radius, final int color) {
|
||||
mPoint = point;
|
||||
mRadius = radius;
|
||||
mColor = color;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* draws a specific radius on the mapview that is handed to it
|
||||
* @param canvas canvas to be drawn on
|
||||
* @param mapView
|
||||
* @param shadow
|
||||
* @param mapView
|
||||
* @param shadow
|
||||
* @param when
|
||||
*/
|
||||
@Override
|
||||
@@ -68,44 +68,43 @@ public class RadiusOverlay extends Overlay{
|
||||
final Point center = new Point();
|
||||
final Point left = new Point();
|
||||
final Projection projection = mapView.getProjection();
|
||||
|
||||
/*
|
||||
* Calculate a geopoint that is "radius" meters away from geopoint point and
|
||||
* convert the given GeoPoint and leftGeo to onscreen pixel coordinates,
|
||||
* relative to the top-left of the MapView that provided this Projection.
|
||||
*/
|
||||
mRadiusPoint = GeoUtils.distanceFrom(mPoint , mRadius);
|
||||
projection.toPixels(mRadiusPoint, left);
|
||||
projection.toPixels(mPoint, center);
|
||||
|
||||
/*
|
||||
* get radius of the circle being drawn by
|
||||
*/
|
||||
int circleRadius = center.x - left.x;
|
||||
if(circleRadius <= 0){
|
||||
circleRadius = left.x - center.x;
|
||||
}
|
||||
|
||||
/*
|
||||
* paint a circle on the map
|
||||
*/
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStrokeWidth(2.0f);
|
||||
paint.setColor(mColor);
|
||||
paint.setStyle(Style.STROKE);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
|
||||
//draw a dot over the geopoint
|
||||
RectF oval = new RectF(center.x - 2, center.y - 2, center.x + 2, center.y + 2);
|
||||
|
||||
/*
|
||||
* Calculate a geopoint that is "radius" meters away from geopoint point and
|
||||
* convert the given GeoPoint and leftGeo to onscreen pixel coordinates,
|
||||
* relative to the top-left of the MapView that provided this Projection.
|
||||
*/
|
||||
mRadiusPoint = GeoUtils.distanceFrom(mPoint , mRadius);
|
||||
projection.toPixels(mRadiusPoint, left);
|
||||
projection.toPixels(mPoint, center);
|
||||
|
||||
/*
|
||||
* get radius of the circle being drawn by
|
||||
*/
|
||||
int circleRadius = center.x - left.x;
|
||||
if(circleRadius <= 0)
|
||||
circleRadius = left.x - center.x;
|
||||
|
||||
/*
|
||||
* paint a circle on the map
|
||||
*/
|
||||
paint.setAntiAlias(true);
|
||||
paint.setStrokeWidth(2.0f);
|
||||
paint.setColor(mColor);
|
||||
paint.setStyle(Style.STROKE);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
|
||||
//draw a dot over the geopoint
|
||||
final RectF oval = new RectF(center.x - 2, center.y - 2, center.x + 2, center.y + 2);
|
||||
canvas.drawOval(oval, paint);
|
||||
|
||||
|
||||
//fill the radius with a nice green
|
||||
paint.setAlpha(25);
|
||||
paint.setStyle(Style.FILL);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
paint.setStyle(Style.FILL);
|
||||
canvas.drawCircle(center.x, center.y, circleRadius, paint);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the selected location
|
||||
* @author ricky barrette
|
||||
@@ -113,12 +112,17 @@ public class RadiusOverlay extends Overlay{
|
||||
public GeoPoint getLocation(){
|
||||
return mPoint;
|
||||
}
|
||||
|
||||
|
||||
public int getZoomLevel() {
|
||||
// GeoUtils.GeoUtils.distanceFrom(mPoint , mRadius)
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTap(GeoPoint p, MapView mapView) {
|
||||
public boolean onTap(final GeoPoint p, final MapView mapView) {
|
||||
mPoint = p;
|
||||
if(this.mListener != null)
|
||||
this.mListener.onLocationSelected(p);
|
||||
if(mListener != null)
|
||||
mListener.onLocationSelected(p);
|
||||
return super.onTap(p, mapView);
|
||||
}
|
||||
|
||||
@@ -126,33 +130,28 @@ public class RadiusOverlay extends Overlay{
|
||||
* @param color
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setColor(int color){
|
||||
public void setColor(final int color){
|
||||
mColor = color;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param location
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public void setLocation(GeoPoint location){
|
||||
public void setLocation(final GeoPoint location){
|
||||
mPoint = location;
|
||||
}
|
||||
|
||||
|
||||
public void setLocationSelectedListener(final OnLocationSelectedListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param radius in meters
|
||||
* @author ricky barrette
|
||||
* @param radius
|
||||
* @param radius
|
||||
*/
|
||||
public void setRadius(int radius){
|
||||
public void setRadius(final int radius){
|
||||
mRadius = radius;
|
||||
}
|
||||
|
||||
public int getZoomLevel() {
|
||||
// GeoUtils.GeoUtils.distanceFrom(mPoint , mRadius)
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setLocationSelectedListener(OnLocationSelectedListener listener) {
|
||||
this.mListener = listener;
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,12 @@ import com.google.android.maps.MapView;
|
||||
public class SkyHookUserOverlay extends BaseUserOverlay{
|
||||
|
||||
private final SkyHook mSkyHook;
|
||||
|
||||
public SkyHookUserOverlay(MapView mapView, Context context) {
|
||||
|
||||
public SkyHookUserOverlay(final MapView mapView, final Context context) {
|
||||
super(mapView, context);
|
||||
mSkyHook = new SkyHook(context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new SkyHookUserOverlay
|
||||
* @param mapView
|
||||
@@ -30,11 +30,16 @@ public class SkyHookUserOverlay extends BaseUserOverlay{
|
||||
* @param followUser
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public SkyHookUserOverlay(MapView mapView, Context context, boolean followUser) {
|
||||
public SkyHookUserOverlay(final MapView mapView, final Context context, final boolean followUser) {
|
||||
super(mapView, context, followUser);
|
||||
mSkyHook = new SkyHook(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFirstFix(final boolean isFistFix) {
|
||||
// unused
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the location provider needs to be disabled
|
||||
* (non-Javadoc)
|
||||
@@ -56,9 +61,4 @@ public class SkyHookUserOverlay extends BaseUserOverlay{
|
||||
mSkyHook.getUpdates();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFirstFix(boolean isFistFix) {
|
||||
// unused
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,23 +11,28 @@ import com.TwentyCodes.android.location.AndroidGPS;
|
||||
import com.google.android.maps.MapView;
|
||||
|
||||
/**
|
||||
* This is the standard version of the UserOverlay.
|
||||
* This is the standard version of the UserOverlay.
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class UserOverlay extends BaseUserOverlay{
|
||||
|
||||
private final AndroidGPS mAndroidGPS;
|
||||
|
||||
public UserOverlay(MapView mapView, Context context) {
|
||||
public UserOverlay(final MapView mapView, final Context context) {
|
||||
super(mapView, context);
|
||||
mAndroidGPS = new AndroidGPS(context);
|
||||
}
|
||||
|
||||
public UserOverlay(MapView mapView, Context context, boolean followUser) {
|
||||
|
||||
public UserOverlay(final MapView mapView, final Context context, final boolean followUser) {
|
||||
super(mapView, context, followUser);
|
||||
mAndroidGPS = new AndroidGPS(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFirstFix(final boolean isFistFix) {
|
||||
// unused
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMyLocationDisabled() {
|
||||
mAndroidGPS.disableLocationUpdates();
|
||||
@@ -38,9 +43,4 @@ public class UserOverlay extends BaseUserOverlay{
|
||||
mAndroidGPS.enableLocationUpdates(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFirstFix(boolean isFistFix) {
|
||||
// unused
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user