Search code examples
androidorientationandroid-mapviewandroid-sensorsazimuth

Determine building your Android device is pointing at


I have a MapView in which I am drawing buildings on using geopoints. Currently, the app determines the closest building to you by just checking your long/lat and comparing to the buildings; but what I want to be able to do is point my device at a building and have that buildings info pop up, even if it isn't the closest building to you.

I have looked up how to accomplish this and I would be using the azimuth direction received from the ORIENTATION sensor listener. I am having trouble using that azimuth direction and determining if it is intercepting one of the buildings on the map.

Would I be using the ORIENTATION sensor and azimuth to accomplish this? How would I go about implementing this? Any help appreciated!


Solution

  • Figured out the solution:

    I used this function(and helper function) to get the bearing from my current location to the closest buildings around me(using a set radius):

    public static float calculateBearing(GeoPoint before, GeoPoint after) {
        Point pBefore = location2Point(before);
        Point pAfter = location2Point(after);
    
        float res = -(float) (Math.atan2(pAfter.y - pBefore.y, pAfter.x
                - pBefore.x) * 180 / Math.PI) + 90.0f;
        if (res < 0)
            return res + 360.0f;
        else
            return res;
    }
    
    public static Point location2Point(GeoPoint aLocation) {
        return new Point((int) (aLocation.getLongitudeE6()),
                (int) (aLocation.getLatitudeE6()));
    }
    

    Once you have the bearings of the buildings around you; you must then compare that to the bearing your phone is positioned at using a SensorEventListener for the TYPE_ORIENTATION sensor to find out which of the closest buildings your pointing at.