在Android中使用纬度和经度值创build一个Location对象

我有一个程序,其中一个位置的纬度和经度值存储在我下载的数据库中。

我想获得这些坐标和我目前的位置之间的距离。

Location类有一个简单的方法来查找两个Location对象之间的距离,所以我想我会用坐标来创build一个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); 

您可以使用其构造函数创build位置,然后设置latutude和longitude值。

 final Location location = new Location("yourprovidername"); location.setLatitude(1.2345d); location.setLongitude(1.2345d);