Get screen coordinates by specific location and longitude (android)

I have an augmented reality application in which I stored information such as metro, gas stations, attractions, etc. with appropriate latitude and longitude.

Now, in accordance with the orientation of the device, I will show a marker for each site in the form of a camera on the device. Like Layar and Wikitude.

It took a three-day search without stopping and did not find anyone who could explain how to solve this problem.

+8
android latitude-longitude augmented-reality
source share
1 answer

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

  • Get your own lat / lon and current compass heading (lat1, lon1 and heading). On iPhone, CLLocation returns them in degrees, but for these calculations they MUST be in radians (i.e., Multiply by PI / 180)
  • Get moons / points of interest (POIs) in radians (lat2 and lon2).
  • Calculate the distance between lat1 / lon1 and lat2 / lon2 using the formula found here: http://www.movable-type.co.uk/scripts/latlong.html
  • Calculate the angle to lat2 / lon2 with respect to the north. This is also described in the link above, but I had a bit of trouble getting this to work, here is the 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 compute x and y. Remember that these coordinates are in three-dimensional space, so we have not finished here because you still need 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 forecast formula, you can calculate the screen x (I did not do y because it was not necessary for my project, but you would need to get the accelerator data and find out if the device is perpendicular to the ground). The projection formula is here (scroll to the bottom): http://membres.multimania.fr/amycoders/tutorials/3dbasics.html

    double screenX = (x * 256) / z

Now you can use this x coordinate to move the image or marker on the screen. Recall a few points:

  • Everything should be in radians
  • The angle from you to the POI in relation to the north is angleBeteweenPoints - currentHeading

(For some reason, I cannot correctly format the code on this computer, so if someone wants to edit this answer, feel free to).

+7
source share

All Articles