我有一个程序,其中一个地点的纬度和经度值存储在数据库中,我下载了这些信息。
我想要获取这些坐标与我的当前位置之间的距离。
Location类有一个简单的方法来查找两个Location对象之间的距离,因此我想创建一个具有这些坐标的Location对象,然后调用该方法。
有没有一种简单的方法可以做到这一点?如果有另一个可靠的、相当简单的方程,不会使事情过于复杂,那也可以。谢谢。
(android.location.Location)
我有一个程序,其中一个地点的纬度和经度值存储在数据库中,我下载了这些信息。
我想要获取这些坐标与我的当前位置之间的距离。
Location类有一个简单的方法来查找两个Location对象之间的距离,因此我想创建一个具有这些坐标的Location对象,然后调用该方法。
有没有一种简单的方法可以做到这一点?如果有另一个可靠的、相当简单的方程,不会使事情过于复杂,那也可以。谢谢。
(android.location.Location)
假设您已经拥有一个位置对象并且包含您当前的位置信息。
Location targetLocation = new Location("");//provider name is unnecessary
targetLocation.setLatitude(0.0d);//your coords of course
targetLocation.setLongitude(0.0d);
float distanceInMeters = targetLocation.distanceTo(myLocation);
static { location.setLatitude(0.0d); location.setLongitude(0.0d); }
。 - Jason Hartleyfinal Location location = new Location("yourprovidername");
location.setLatitude(1.2345d);
location.setLongitude(1.2345d);
我重新回答一遍,因为很多像我一样的人不知道"providername"
实际上是什么。以下代码回答了这个问题:
Location location = new Location(LocationManager.GPS_PROVIDER);
location.setLatitude(23.5678);
location.setLongitude(34.456);
在这里,我使用LocationManager.GPS_PROVIDER
作为提供程序名称。
LocationManager.GPS_PROVIDER
,还有其他可供考虑的替代方案吗? - ban-geoengineeringlocation
对象是手动创建的,那么使用空字符串而不是LocationManager.GPS_PROVIDER
、LocationManager.PASSIVE
或LocationManager.NETWORK_PROVIDER
更为合适,对吗? - ban-geoengineeringKotlin版本:
val location = Location("")
location.latitude = 1.2345 // data type is Double
location.longitude = 1.2345 // data type is Double