如何比较Android中的地理位置?

时间:2015-04-03 09:00:45

标签: android gps

如何比较保存的(在数据库中)地理位置(经度和纬度)与当前使用GPS.i尝试以下链接,但它不适合我,输出错误。请帮助我我是新的在Android中。

How to use coarse location to compare with saved location in database

我的要求与ANS中解释的完全相同。 我做了

浮动半径= 150; //以米为单位的距离

  double latitude_new= gps.getLatitude();
  double longitude_new = gps.getLongitude();


  Location location1= new Location("gpslocation");
  location1.setLatitude(21.xxxxxxx);
  location1.setLongitude(78.xxxxxxx);

  Location location2= new Location("gpslocation");
  location2.setLatitude(latitude_new);
  location2.setLongitude(latitude_new);

  float distance = location2.distanceTo(location1);
  distance=Math.abs(distance);
  //comparing two distance and radius

  if (distance <= radius){
      Toast.makeText(MyActivity.this, "At home", Toast.LENGTH_SHORT).show();
  }else {
      Toast.makeText(MyActivity.this,"Not in Home",Toast.LENGTH_SHORT).show();
  }

问题是我只得到&#34;不在家里&#34;在SAME位置,我有硬编码,甚至在其他位置(硬编码除外)。

谢谢

1 个答案:

答案 0 :(得分:0)

您还提供经度纬度,编辑代码如下,然后尝试

location2.setLongitude(longitude_new);