I have a Augmented Reality App that simply shows the camera view and
get's the user GPS position, azimuth, direction, inclination, etc...
Also i have stored the GPS position of a famous building of my City.
Now i need that when the user faces to the direction of the Building,
a textview ("Building") or a image (the easiest one) must be draw on
the screen in the Building position on the camera.

**I know how to check if the phone is facing the building, i can do
with this code:**

    float a =loc.bearingTo(BuildingLocation);
    float b =azimuth;
    float d = Math.abs((a - b)) % 360;
    float r = d > 180 ? 360 - d : d;

Ok, that code gives me the degree diference between what is facing the
phone and the building GPS position. Then, the most the user is facing
the building, the little will be the `r` value. The problem now is
that when the user is at more distance of the building... the angle of
vision is higher, and i dont know how, when and where i have to draw
the textview that i want to put exactly on the building position.

¿How can i do it? I read some accelerometer/compass/gps tutorials but
all of them are poorly explained and didn't show me how to do this,
for example: [http://www.devx.com/wireless/article/42482/1954][1] this
tutorial is very poorly explained and it didn't works when i use the
code, Also doesn't gives me explanations of how to show a point when u
face the object and how to calculate distances

Good tutorials for doing this, and code examples, are welcome.

This is the code i have done. Captures user GPS position (`location`),
azimuth, direction, inclination, etc... and also have stored the
position of the building (`BuildingLocation` `BuildingLat` &
`BuildingLon`).

Help will be appreciated

The code (CustomCameraView class is a default camera view class, the
code for that class is in the tutorial of the link i posted some lines
up):

    public class AugmentedRealitySampleActivity extends Activity {
        private CustomCameraView cv=null;
        private TextView tv1;
        private TextView tv2;
        private TextView tv3;

        public static SensorManager sensorMan;

        private Location location; //gps
        public volatile float inclination; //accelerometer

        double BuildingLat;
        double BuildingLon;
        Location BuildingLocation;

        private static final int matrix_size = 16;
        float[] R = new float[matrix_size];
        float[] outR = new float[matrix_size];
        float[] I = new float[matrix_size];
        float[] values = new float[3];
        private float[] mags;
        private float[] accels;
        private float azimuth;
        private float incValue;

        LocationManager mLocationManager;
        MyLocationListener mLocationListener;

        public void onCreate(Bundle savedInstanceState)
        {
                   super.onCreate(savedInstanceState);
                   cv = new CustomCameraView(this.getApplicationContext());
                   FrameLayout rl = new 
FrameLayout(this.getApplicationContext());
                   LinearLayout ll= new 
LinearLayout(this.getApplicationContext());
                   ll.setOrientation(LinearLayout.VERTICAL);

                   setContentView(rl);
                   rl.addView(cv);
                   rl.addView(ll);

                   tv1=new TextView(getApplicationContext());
                   tv2=new TextView(getApplicationContext());
                   tv3=new TextView(getApplicationContext());

                   ll.addView(tv1);
                   ll.addView(tv2);
                   ll.addView(tv3);

                   sensorMan = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
                   sensorMan.registerListener(SensorListener,
sensorMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_UI);
               sensorMan.registerListener(SensorListener,
sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);

                   mLocationListener = new MyLocationListener();
                   mLocationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 3, mLocationListener);

                   //getLatitude(); getLongitude(); bearingTo(); distanceTo();
                   tv1.setText("Test1");
                   tv1.setBackgroundColor(Color.BLACK);
                   tv2.setText("Test2");
                   tv2.setBackgroundColor(Color.BLACK);
                   tv3.setText("Test3");

                   //location of the Building to face:
                   String coordinates2[] = {"39.4643", "-0.36038"};
               BuildingLat = Double.parseDouble(coordinates2[0]);
               BuildingLon = Double.parseDouble(coordinates2[1]);
               BuildingLocation = new Location(LocationManager.GPS_PROVIDER);
               BuildingLocation.setLatitude(BuildingLat);
               BuildingLocation.setLongitude(BuildingLon);
        }

        ///////////////////////
GPS /////////////////////////////////////////////
    private class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location loc) {
                if (loc!=null)
                {
                        tv1.setText("Location= " + loc.getLatitude()+"
"+loc.getLongitude());
                        tv2.setText("Distance to Building:
"+loc.distanceTo(BuildingLocation));
                        tv2.append("\nBearingTo: 
"+loc.bearingTo(BuildingLocation));
                        location=loc; //stores current location
                }
                else
                        tv1.setText("null :(");
        }
        public void onProviderDisabled(String provider) {       }
        public void onProviderEnabled(String provider) {       }
        public void onStatusChanged(String provider, int status,
Bundle extras) {       }
    }

        //////////////////// COMPASS &
ACCELEROMETER ////////////////////////////
        SensorEventListener SensorListener = new SensorEventListener()
        {
                public void onAccuracyChanged(Sensor arg0, int accuracy){ }
                public void onSensorChanged(SensorEvent event)
                {
                        if (event.accuracy == 
SensorManager.SENSOR_STATUS_UNRELIABLE)
                                return;
                        switch (event.sensor.getType())
                        {
                                case Sensor.TYPE_MAGNETIC_FIELD:
                                        mags = event.values.clone();
                                        break;
                                case Sensor.TYPE_ACCELEROMETER:
                                        accels = event.values.clone();
                                        break;
                        }
                        if (mags != null && accels != null)
                        {
                                SensorManager.getRotationMatrix(R, I, accels, 
mags);
                                //Correct if screen is in Landscape
        
SensorManager.remapCoordinateSystem(R,SensorManager.AXIS_X,SensorManager.AXIS_Z,
outR);
                                SensorManager.getOrientation(outR, values);
                                incValue=SensorManager.getInclination(I);
                                azimuth = (float) 
Math.round((Math.toDegrees(values[0]))*2)/2;
                                //Adjust the range: 0 < range <= 360 (from: 
-180 < range <= 180).
                                azimuth = ( azimuth + 360)%360; // alternative: 
mAzimuth =
mAzimuth>=0 ? mAzimuth : mAzimuth+360;
                                tv3.setText("Azimuth (deg)= " + azimuth);
                                tv3.append("\n Azimuth (rad)= " + values[0]);
                                inclination=-Math.round((float) 
(values[1]*(360/(2*Math.PI))));
                                tv3.append("\n Pitch (rad)= " + values[1]);
                                tv3.append("\n Pitch (deg)= " + inclination);
                                tv3.append("\n Roll (rad)= " + 
(values[2]*(360/(2*Math.PI))));
                        }
                }
        };
    }

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

Reply via email to