This is why I suggest you prototype it as a desktop "toy" program in
plain Java.  You can feed in numbers and see what you get out -- see
if the numbers make sense.  You can even plot them out on a map to
verify them.

Re the formula, it looks to be approximately correct (assuming
"values" is in degrees).  When dealing with compass points there's
always the issue of where the zero origin is, and whether you're going
+ or -, but those are nits to work out.

You do have a basic (but not fatal) error in that the formula is only
really accurate when you're near the equator and the two points are
relatively close together.  Near a pole the result would be off
(pointing too far east or west).  But the amount of error below, say,
the 50th parallel would be fairly small -- less than "eyeball"
accuracy in most cases.

(And of course it's questionable as to how accurate the compass in the
device is in the first place.  Even if it's auto-corrected for
magnetic declination, a small magnetic compass is subject to
substantial interference from nearby objects.)

On Sep 16, 1:58 pm, Pedro Teixeira <[email protected]> wrote:
> I'm actually not positivlly sure about the algorythm since it will  
> return always the same value if I'm on the same position... so it  
> doesn't make sense because it should depend on the device  
> orientation... I'm using now the azimuth.. because it will depend on  
> the orientation of the device.. at least the line is moving.. I guess  
> is just a mathematical thing now ..ohh the math :S
>
> float angleDegrees = (float) Math.abs(Math.toDegrees(Math.atan2
> (picLongitude-mLongitude,  picLatitude-mLatitude))-event.values[0]);
>
> On Sep 16, 2010, at 5:41 PM, DanH wrote:
>
>
>
> > Have you done any debugging?  Looked at the raw sensor readings to
> > make sure they make sense?  Implemented the algorithm in Java on a
> > desktop, fed in sensor numbers, and checked whether it produced what
> > you expected?
>
> > On Sep 16, 11:26 am, Pedro Teixeira <[email protected]> wrote:
> >> 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.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> Pedro Teixeira
>
> www.pedroteixeira.org

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to