0

搜索片段

参考图片。我正在尝试构建一个 POC,其中在搜索时用户当前位置包含在搜索结果中(不是所有用户,而是只有在搜索中返回的用户。所有这些用户都已经登录到我的应用程序中,我已经知道他们的用户 ID . 我尝试运行一个服务,以便我可以在后台获取用户位置并使用以下技术将其广播到我的搜索片段,除了我使用服务方法而不是 IntentService:

public class MyIntentService extends IntentService {

  //ACTION should include application package convention, just to show that this can
  //be any string
  public static final String ACTION = "mycustomactionstring";

  public MyIntentService() {
    super("MyIntentService");
    // TODO Auto-generated constructor stub
    Log.d("sohail", "service started");
  }

  @
  Override
  protected void onHandleIntent(Intent arg0) {
    // TODO Auto-generated method stub

    Log.d("sohail", "onHandleIntent called");
    Intent in = new Intent(ACTION); //you can put anything in it with putExtra
    Log.d("sohail", "sending broadcast");
    LocalBroadcastManager.getInstance(this).sendBroadcast( in );


  }

}

问题是我如何在 Android 中反映仅在搜索结果中提供特定用户的当前位置?Android 中是否有更好的方法允许我们为所有登录用户中的特定用户执行类似 user.getLastLocation 的操作?这里有什么好的方法吗?

我基本上在寻找的是一种在 Android 中轻松说出“给我在搜索结果中返回的特定用户的当前位置”的方式这些用户是所有登录用户的一部分

我想跟踪所有用户的位置并将其保存在数据库中并将其与搜索结果中返回的用户进行比较,但这似乎不是理想的方式?

在这种情况下有人可以提出更好的建议吗?任何帮助将不胜感激。

4

1 回答 1

0

您可以使用网络服务,每个用户每 30 秒或 60 秒在网络服务器上发送当前位置,然后使用网络服务调用更新服务器,您将获得用户的当前纬度

查找两个纬度之间的距离,如下所示。

Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);

Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);

float distanceInMeters = loc1.distanceTo(loc2);

参考: http: //developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)

希望以上解决方案对您有所帮助。

让我知道您是否需要我方的更多帮助。

于 2015-02-19T05:41:25.570 回答