Android 获取当前位置半径内的地址或LatLng
我正在开发一个应用程序,它将显示用户当前位置50米半径范围内的“事件”。我会从在线数据库中获取这些事件。这些事件包含一个LatLng和来自DB的地址 我已经实现了位置更新 我的问题是,哪种方法更好 一种方法: 我使用Android 获取当前位置半径内的地址或LatLng,android,google-maps,parsing,Android,Google Maps,Parsing,我正在开发一个应用程序,它将显示用户当前位置50米半径范围内的“事件”。我会从在线数据库中获取这些事件。这些事件包含一个LatLng和来自DB的地址 我已经实现了位置更新 我的问题是,哪种方法更好 一种方法: 我使用addCircle在当前位置绘制圆。然后在当前位置更改时删除并重新绘制这些圆 最困难的部分是如何在50米范围内获得位置或lat/lang 然后我将查询我的数据库并返回我的事件 我考虑过的另一种方法是地理围栏。我现在还在学习。 也许我会在我的当前位置周围画一个地理围栏,并根据需要进行更
addCircle
在当前位置绘制圆。然后在当前位置更改时删除并重新绘制这些圆
最困难的部分是如何在50米范围内获得位置或lat/lang
然后我将查询我的数据库并返回我的事件
我考虑过的另一种方法是地理围栏。我现在还在学习。
也许我会在我的当前位置周围画一个地理围栏,并根据需要进行更新 您需要一个名为
InRadius(centerX, centerY, radius, x, y)
{
Return (radius * 111000f) >= sqrt((x-centerX )^2 +(y-centerY) ^2)
}
中心是用户位置。但您必须调用事件表中的每条记录,将lat输入y,将long输入x您需要一个名为
InRadius(centerX, centerY, radius, x, y)
{
Return (radius * 111000f) >= sqrt((x-centerX )^2 +(y-centerY) ^2)
}
public static LatLng getLocation(double lon, double lat, int radius)
{
Random random = new Random();
// Convert radius from meters to degrees
double radiusInDegrees = radius / 111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);
// Adjust the x-coordinate for the shrinking of the east-west distances
double new_x = x / Math.cos(lat);
double foundLongitude = new_x + lon;
double foundLatitude = y + lat;
System.out.println("Longitude: " + foundLongitude + " Latitude: "
+ foundLatitude);
return new LatLng(foundLatitude, foundLongitude);
}
中心是用户位置。但您必须调用事件表中的每条记录,将lat输入y,将long输入x
public static LatLng getLocation(double lon, double lat, int radius)
{
Random random = new Random();
// Convert radius from meters to degrees
double radiusInDegrees = radius / 111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);
// Adjust the x-coordinate for the shrinking of the east-west distances
double new_x = x / Math.cos(lat);
double foundLongitude = new_x + lon;
double foundLatitude = y + lat;
System.out.println("Longitude: " + foundLongitude + " Latitude: "
+ foundLatitude);
return new LatLng(foundLatitude, foundLongitude);
}
然后在需要的地方调用函数。:)
->位置=您当前的位置->50=你想给什么 希望你得到你的答案:) 然后在需要的地方调用函数。:) ->位置=您当前的位置
->50=你想给什么
希望你得到答案:)我建议你使用圆,用圆latlong计算所有latlong的距离,并检查所有latlong是否在半径内。我建议你使用圆,用圆latlong计算所有latlong的距离,并检查所有latlong是否在半径内。这是正确的。X是长的,y是lat,但是如果有帮助的话,您可以重命名这些参数。我编辑了我的答案。我修正了一个错误。嗯,这些也可以。由于
LatLng
格式是十进制的,我也在尝试另一种方法,因为我在DB API中看到了一个helper方法,您可以执行小于/大于的查询,如何获得给定中心和半径内的上/下限x和y(4个值)。好吧,在sql中类似<代码>从事件中选择*,其中(radius*111000.0)>=sqrt((x-centerX)^2+(y-centerY)^2)我正在使用Parse SDK,但我会尝试类似的方法sql@Smaran是的,是以米为单位,没错。X是长的,y是lat,但是如果有帮助的话,您可以重命名这些参数。我编辑了我的答案。我修正了一个错误。嗯,这些也可以。由于LatLng
格式是十进制的,我也在尝试另一种方法,因为我在DB API中看到了一个helper方法,您可以执行小于/大于的查询,如何获得给定中心和半径内的上/下限x和y(4个值)。好吧,在sql中类似<代码>从事件中选择*,其中(radius*111000.0)>=sqrt((x-centerX)^2+(y-centerY)^2)我正在使用Parse SDK,但我会尝试类似的方法sql@Smaran,是的,它在metersI中,我没有在你的函数中使用random。你能解释一下吗?最终的函数输出间接地依赖于这些随机数。也许那个是你们忘了带回去的测试代码?随机函数会在我的位置附近给我1个随机LatLng。这不取决于它所依赖的是什么。谢谢你。。很好的回答,我没有在你的函数中使用random。你能解释一下吗?最终的函数输出间接地依赖于这些随机数。也许那个是你们忘了带回去的测试代码?随机函数会在我的位置附近给我1个随机LatLng。这不取决于它所依赖的是什么。谢谢你。。很好的回答