Android simple augmented reality with GPS

非 Y 不嫁゛ 提交于 2019-12-11 00:49:18

问题


I want to develop a simple AR android app.

I was able to find code to get the: azimuth,pitch and roll, and think that I got it right.

I can't find how to be able to display an image on top of the "camera preview" according to a GPS location.

I have my (latitude,longitude) coordinates and a set of other (latitude,longitude) coordinates .

What I want is the ability to display "markers" on those coordinates when the user points the camera at them.

How do I combine the coordinates and azimuth,pitch and roll to achieve that?

I don't mind of using 3rd party stuff - but I do need it to FREE.

Thanks


回答1:


The following functions should help. Probably all you have to do is converting the code.

setObjTranslation() computes the marker coordinates/position relative to the player position. As you can see inside the function, a tmpx and tmpy are computed. Insted of using the above code (translation = ...(x,y,z)) you will have something like:

markerPos.x = tmpx;
markerPos.y = tmpy;

You don't need the third value, the z translation, unless you are placing the object/marker in a 3D scene.

Then, setDistanceToPlayer() computes the actual distance between the player position and the marker position (in meters). If you google search haversine you will find out more about this function. Basically it computes the distance between 2 GPS locations.

The last function, markerPlayerAngle() computes the angle (in degrees) between 2 GPS coordinates. Therefore, based on the device orientation, you will be able to determine if the marker is "visible", in other words if the marker is in the FOV of the device...

function setObjTranslation() {
                    if (!arObjectIsVisible() || vec2fUndefined(playerPos) || vec2fUndefined(objectPosition)) return;
                    tmpx = calcmetdistance(playerPos.x, playerPos.y, objectPosition.x, playerPos.y);
                    tmpy = calcmetdistance(playerPos.x, playerPos.y, playerPos.x, objectPosition.y);

                    arObjectPos.translation = new SFVec3f ( tmpx, tmpy, 0 );
                    setDistanceToPlayer();
                }
function calcmetdistance(lat1, lon1, lat2, lon2) {
                    R       = 6371000;
                    lat1    *=Math.PI/180;
                    lon1    *=Math.PI/180;
                    lat2    *=Math.PI/180;
                    lon2    *=Math.PI/180;
                    d       = Math.acos (   
                                            Math.sin(lat1)*Math.sin(lat2) + 
                                            Math.cos(lat1)*Math.cos(lat2) *
                                            Math.cos(lon2-lon1)
                                        ) * R;
                    return d; 
                }

function setDistanceToPlayer() {
                    distance = haversine_m(objectPosition.x, objectPosition.y, playerPos.x, playerPos.y).toFixed();
                    arObjDistanceTranslation.rotation = new SFRotation( 0, 1, 0, markerPlayerAngle(objectPosition.x, objectPosition.y, playerPos.x, playerPos.y));
                }

function haversine_m(lat1, long1, lat2, long2) {
                    d2r     = Math.PI / 180;
                    d_latt  = (lat2 - lat1) * d2r;
                    d_long  = (long2 - long1) * d2r;
                    a       = Math.sin(d_latt/2)*Math.sin(d_latt/2) + Math.cos(lat1 * d2r) * Math.cos(lat2 * d2r) * Math.sin(d_long/2)*Math.sin(d_long/2);
                    c       = 2 * Math.atan2(Math.sqrt(a),  Math.sqrt(1-a));
                    return 6371 * 1000 * c;
                }

function markerPlayerAngle(markerLat, markerLong, playerLat, playerLong) {
                    dy      = markerLat - playerLat;
                    dx      = Math.cos(Math.PI/180*playerLat)*(markerLong - playerLong);
                    return Math.atan2(dy, dx);
                }


来源:https://stackoverflow.com/questions/21928670/android-simple-augmented-reality-with-gps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!