通过特定位置和经度获取屏幕坐标(android)

发布于 2024-10-22 21:22:49 字数 148 浏览 4 评论 0原文

我有一个增强现实的应用程序,其中存储了地铁、加油站、名胜古迹等信息以及相应的纬度和经度。

现在,根据设备的方向,我将在设备的相机视图中显示每个站点的标记。类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2024-10-29 21:22:49

由于关于这个主题的信息非常稀疏,而且我最近在 iPhone 上解决了这个问题,所以我想我会与任何可以使其在 Android 上工作的人分享我的方法(除了数学函数之外,这个答案中没有任何特定于 iPhone 的内容) sin、cos 和 fmod,可以在 java.lang.Math 中找到)。这些是我采取的步骤:

  • 获取您自己的纬度/经度和当前的罗盘航向(纬度1、经度1 和航向)。在 iPhone 上,CLLocation 以度为单位返回这些值,但对于这些计算,它们必须以弧度为单位(即乘以 PI/180)
  • 获取以弧度表示的兴趣点 (POI) 的纬度/经度(lat2 和隆2)。
  • 使用此处找到的公式计算 lat1/lon1 和 lat2/lon2 之间的距离: http: //www.movable-type.co.uk/scripts/latlong.html
  • 计算 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 坐标在屏幕上移动图像或标记。记住几点:

  • 一切都必须以弧度为单位
  • 从你到POI相对于北的角度是angleBeteweenPoints - currentHeading

(由于某种原因我无法在这台计算机上正确格式化代码,所以如果有人想编辑这个答案,请感觉自由的)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文