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

How to Compaire Geolocations In Android?

如何比较已保存(在数据库中)的地理位置(经度和纬度)与当前使用 GPS.i 获得的地理位置(经度和纬度)尝试遵循 link 但它对我不起作用,出错了 output.Please 帮助我,我是 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();
  }

问题是我在我硬编码的相同位置甚至在其他位置(除了硬编码)只得到 "Not in Home"。

谢谢

您也为经度提供了纬度,按如下方式编辑您的代码并尝试,

location2.setLongitude(longitude_new);