Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen coordinates by specific location and longitude (android)

I have an application of augmented reality in which I have stored information such us metro, gas stations, places of interest, etc. with the corresponding latitude and longitude.

Now, according to the orientation of the device, I would show a marker for each site in the camera view of the device. Similar to Layar and Wikitude.

It takes three days searching without stopping and have not found anyone to explain how to solve this problem.

like image 955
Adrià Carro Avatar asked Mar 15 '11 16:03

Adrià Carro


People also ask

Can I get longitude and latitude on my phone?

Android: Open Google Maps; it will zoom to your approximate location. Press and hold on the screen to drop a pin marker. Click on the dropped pin; latitude and longitude will be displayed below the map.

How do you find a location with degrees minutes and seconds?

Open Google Earth. In the Search box in the left-hand panel, enter coordinates using one of these formats: Decimal Degrees: such as 37.7°, -122.2° Degrees, Minutes, Seconds: such as 37°25'19.07"N, 122°05'06.24"W.


1 Answers

Since information on this topic is very sparse, and I recently solved this problem on the iPhone, I thought I would share my method for anyone that can make it work with Android (there's nothing really specific to iPhone in this answer except for the Math functions sin, cos, and fmod, which can be found in java.lang.Math). These are the steps I took:

  • Obtain your own lat/lon and your current compass heading (lat1, lon1 and heading). On the iPhone, CLLocation returns these in degrees, but for these calculations they MUST be in radians (i.e. multiply by PI/180)
  • Obtain lat/lon of Points of Interest (POI) in radians (lat2 and lon2).
  • Calculate the distance between lat1/lon1 and lat2/lon2 using formula found here: http://www.movable-type.co.uk/scripts/latlong.html
  • Calculate angle to lat2/lon2 in relation to north. This is also described in the link above but I had a little bit of trouble getting this to work, here is C code for this:

    double latDelta = (lat2 - lat1);
    double lonDelta = (lon2 - lon1);
    double y = sin(lonDelta) * cos(lat2);
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2)* cos(lonDelta);
    double angle = atan2(y, x); //not finished here yet
    double headingDeg = compass.currentHeading;
    double angleDeg = angle * 180/PI;
    double heading = headingDeg*PI/180;
    angle = fmod(angleDeg + 360, 360) * PI/180; //normalize to 0 to 360 (instead of -180 to 180), then convert back to radians
    angleDeg = angle * 180/PI;

  • Using standard trigonometry, I calculate x and y. Remember, these coordinates are in 3D space, so we are not finished here yet because you still have to map them to 2D:

    x = sin(angle-heading) * distance;
    z = cos(angle-heading) * distance; //typically, z faces into the screen, but in our 2D map, it is a y-coordinate, as if you are looking from the bottom down on the world, like Google Maps

  • Finally, using the projection formula, you can calculate screen x ( I didn't do y because it was not necessary for my project, but you would need to get accelerator data and figure out if the device is perpendicular to the ground). The projection formula is found here (scroll to the very bottom): http://membres.multimania.fr/amycoders/tutorials/3dbasics.html

    double screenX = (x * 256) / z

Now you can use this x coordinate to move an image or a marker on your screen. Remember a few points:

  • Everything must be in radians
  • The angle from you to the POI relative to North is angleBeteweenPoints - currentHeading

(For some reason I can't properly format the code on this computer, so if anyone wants to edit this answer, feel free).

like image 188
Mirkules Avatar answered Oct 14 '22 18:10

Mirkules