Hi everyone..I'm trying to implement spomething that I don't even
know
if it's possible..
So I have this class which implements a SensorEventListener like
this:
private SensorEventListener mySensorEventListener = new
SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int
accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
myCompass.updateDirection((float)event.values[0]);
}
};
And a class that handles the way the compass looks like this:
public static class compassLook extends View {
private Paint paintPointer = new Paint
(Paint.ANTI_ALIAS_FLAG);
private Paint paintCircle = new Paint
(Paint.ANTI_ALIAS_FLAG);
private Paint paintLeters = new Paint
(Paint.ANTI_ALIAS_FLAG);
private boolean firstDraw;
private float direction = 0 ;
public compassLook(Context context) {
super(context);
init();
}
public compassLook(Context context, AttributeSet
attrs) {
super(context, attrs);
init();
}
public compassLook(Context context, AttributeSet
attrs, int
defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
paintPointer.setStyle(Paint.Style.STROKE);
paintPointer.setStrokeWidth(3);
paintPointer.setColor(Color.argb(255, 209, 114, 2));
paintCircle.setStyle(Paint.Style.STROKE);
paintCircle.setStrokeWidth(2);
paintCircle.setColor(Color.argb(150, 255, 255, 255));
paintLeters.setStyle(Paint.Style.STROKE);
paintLeters.setStrokeWidth(1);
paintLeters.setColor(Color.argb(230, 255, 255, 255));
paintLeters.setTextSize(12);
firstDraw = true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int
heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize
(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
protected void onDraw(Canvas canvas) {
int cxCompass = (getMeasuredWidth()/2);
int cyCompass = (getMeasuredHeight()/2);
float radiusCompass;
if(cxCompass > cyCompass){
radiusCompass = (float) (cyCompass * 0.9);
}
else{
radiusCompass = (float) (cxCompass * 0.9);
}
canvas.drawCircle(cxCompass-50, cyCompass,
radiusCompass,
paintCircle);
if(!firstDraw){
// Desenho da linha que aponta
canvas.drawLine(cxCompass-50, cyCompass,
(float)(cxCompass-50 + radiusCompass * Math.sin
((double)(-
direction) * 3.14/180)),
(float)(cyCompass - radiusCompass * Math.cos
((double)(-direction)
* 3.14/180)),
paintPointer);
double mLatitude = (mLocation.getLatitude() /
1E6); // I get this
value from a LM request
double mLongitude = (mLocation.getLongitude() /
1E6); // I get this
value from a LM request
double picLatitude = Double.parseDouble
(picLatitudeString); // I get
this value from a bundle
double picLongitude = Double.parseDouble
(picLongitudeString); // I
get this value from a bundle
float direction = (float) Math.toDegrees(Math.atan2
(picLongitude-
mLongitude, picLatitude-mLatitude));
float distancia = (float) (Math.sqrt
(Math.pow(mLatitude-
picLatitude, 2) + Math.pow(mLongitude-picLongitude,2)))/2;
//Desenho do texto que indica a distancia
canvas.drawText(String.valueOf(distancia) + "Km",
cxCompass-30,
cyCompass+20, paintLeters);
}
}
public void updateDirection(float dir)
{
firstDraw = false;
direction = dir;
invalidate();
}
}
This classes work perfectlly showing me a little circle with a line
pointing the North.
What I would like to do, is ..given this 2 points in space
(mLatitude,
mLongitude) and (picLatitude, picLongitude) which are the user
position, and a certain picture position. I'd like the compass to
point to the position of the picture (picLatitude,picLongitude) from
the position they are in the moment (mLatitude,mLongitude)
I came to the trignometry formula that calculates this..being:
float direction = (float) Math.toDegrees(Math.atan2(picLongitude-
mLongitude, picLatitude-mLatitude));
But when I use it on the updateDirection, the compass line stays
static.. doesn't moves just like it moved when the direction was 0
(north)
Do you have any ideia how can I implement this so that the compass is
always pointing the picture position and not the north?
Thank you very much..if you didnt understood what I meant here,
basically I want a line that points me to an Latitude,Longitude
position other than the north itself.