我有一个程序,其中一个位置的纬度和经度值存储在一个数据库中,我下载了这个数据库。
我想知道这些坐标和我现在的位置之间的距离。
Location类有一个简单的方法来查找两个Location对象之间的距离,所以我想我应该用坐标创建一个Location对象,然后调用该方法。
有什么简单的方法可以做到这一点吗?此外,如果有另一个可靠的、相当简单的方程式,它不会把事情搞得太混乱,那也是可行的。谢谢。
(android.location.Location)
发布于 2013-08-01 09:52:33
假设您已经有一个具有当前位置的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);
发布于 2013-08-01 09:49:29
您可以使用their constructor创建位置,然后设置纬度和经度值。
final Location location = new Location("yourprovidername");
location.setLatitude(1.2345d);
location.setLongitude(1.2345d);
发布于 2018-06-13 14:07:33
我再次回答这个问题,因为很多像我一样的人不知道"providername"
到底是什么。下面的代码回答了这个问题:
Location location = new Location(LocationManager.GPS_PROVIDER);
location.setLatitude(23.5678);
location.setLongitude(34.456);
在这里,我使用LocationManager.GPS_PROVIDER
作为提供程序名称。
https://stackoverflow.com/questions/17983865
复制相似问题