gpt4 book ai didi

java - 如何在 Google map 上存储和检索地点

转载 作者:行者123 更新时间:2023-11-30 01:13:20 25 4
gpt4 key购买 nike

我希望为我的一个大学项目开发​​一个应用程序,为餐厅提供注册和存储一般详细信息、菜单及其餐厅位置的服务,然后我想在 Android 应用程序上检索这些餐厅位置我正在根据用户的位置进行工作,这样当客户打开应用程序时,就会列出该用户附近 5 英里半径范围内的餐馆,

对于连接到同一个数据库的餐馆老板和顾客来说,这将是两个不同的应用程序。

我想要一些关于如何完成这项任务的指示,如果我在错误的方向上完成这项任务,请随时指出最佳实践。

谢谢!

最佳答案

餐厅的位置将以纬度和经度的组合形式存储在您的数据库中。

当客户打开应用程序时,您必须获取客户的当前位置并查询您的数据库以检查用户/客户的当前位置与餐厅位置之间的距离是否小于 5 英里。谷歌已经提供了实现该功能的方法。

假设您将用户的位置表示为:

Location userLocation = new Location("");
userLocation.setLatitude(latitude1);
userLocation.setLongitude(longitude1);

这就是您一次表示从数据库中查询的餐厅位置的方式。

Location restaurantLocation = new Location("");
restaurantLocation.setLatitude(latitude2);
restaurantLocation.setLongitude(longitude2);

这样你就可以计算出以米为单位的距离。

float distanceInMeters = userLocation.distanceTo(restaurantLocation);

您可以按如下方式适当转换为英里:

float distanceInMiles = distanceInMeters / 1.60934

然后检查它是否超过 5 英里。如果条件返回 true,则在 map 中用餐馆的坐标标记该位置。

if (distanceInMiles > 5)
{
// code to set the marker with the restaurant's coordinates on the map.
}

您可以定期执行此测试(比如每 5 分钟一次),以找到用户附近 5 英里半径范围内的餐馆。

关于java - 如何在 Google map 上存储和检索地点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38209474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com