首页 文章

使用纬度和经度值在Android中创建位置对象

提问于
浏览
103

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

我想得到这些坐标和我当前位置之间的距离 .

Location类有一个简单的方法来查找两个Location对象之间的距离,所以我想我会用坐标创建一个Location对象,然后调用该方法 .

是否有捷径可寻?此外,如果还有另一个可靠,相当简单的方程式,它不会使事情变得太混乱,那也会起作用 . 谢谢 .

(android.location.Location)

3 回答

  • 29

    假设您已经拥有一个包含当前位置的位置对象 .

    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);
    
  • 244

    您可以使用their constructor创建位置,然后设置latutude和longitude值 .

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

    我正在回答这个问题,因为像我这样的很多人不知道 "providername" 究竟是什么 . 下面的代码回答了问题:

    Location location = new Location(LocationManager.GPS_PROVIDER);
    location.setLatitude(23.5678);
    location.setLongitude(34.456);
    

    我在这里使用 "LocationManager.GPS_PROVIDER" 有提供者 .

相关问题