通过特定位置和经度获取屏幕坐标(android)
我有一个增强现实的应用程序,其中存储了地铁、加油站、名胜古迹等信息以及相应的纬度和经度。
现在,根据设备的方向,我将在设备的相机视图中显示每个站点的标记。类似于 Layar 和 Wikitude。
找了三天没有间断,也没有找到人解释如何解决这个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于关于这个主题的信息非常稀疏,而且我最近在 iPhone 上解决了这个问题,所以我想我会与任何可以使其在 Android 上工作的人分享我的方法(除了数学函数之外,这个答案中没有任何特定于 iPhone 的内容) sin、cos 和 fmod,可以在 java.lang.Math 中找到)。这些是我采取的步骤:
计算 lat2/lon2 相对于北的角度。上面的链接中也对此进行了描述,但我在使其正常工作时遇到了一些麻烦,这里是 C 代码:
双 latDelta = (lat2 - lat1);
双 lonDelta = (lon2 - lon1);
双 y = sin(lonDelta) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2)* cos(lonDelta);
双角 = atan2(y, x); //这里还没完成
double headerDeg = compass.currentHeading;
double angleDeg = 角度 * 180/PI;
双标题=headingDeg*PI/180;
角度 = fmod(angleDeg + 360, 360) * PI/180; //标准化为0到360(而不是-180到180),然后转换回弧度
angleDeg = angle * 180/PI;
使用标准三角学,我计算 x 和 y。请记住,这些坐标位于 3D 空间中,因此我们还没有完成,因为您仍然需要将它们映射到 2D:
x = sin(角度航向) * 距离;
z = cos(航向角度) * 距离; //通常,z 面朝屏幕,但在我们的 2D 地图中,它是 y 坐标,就好像您从下往上看世界一样,如 Google 地图
最后,使用投影公式,您可以计算屏幕 x (我没有执行 y,因为这对我的项目来说不是必需的,但您需要获取加速器数据并确定设备是否垂直于地面)。投影公式可以在这里找到(滚动到最底部):http://membres。 multimania.fr/amycoders/tutorials/3dbasics.html
double screenX = (x * 256) / z
现在您可以使用此 x 坐标在屏幕上移动图像或标记。记住几点:
(由于某种原因我无法在这台计算机上正确格式化代码,所以如果有人想编辑这个答案,请感觉自由的)。
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:
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:
(For some reason I can't properly format the code on this computer, so if anyone wants to edit this answer, feel free).